Reverse geocoding is not working in some android devices?

后端 未结 2 456
走了就别回头了
走了就别回头了 2021-01-21 13:51

I am developing an application on maps, am not able to get address in this mobile, its android version is 4.3, like below --

相关标签:
2条回答
  • 2021-01-21 14:07

    Geocoder is not working in some devices. So, we need to create a custom Geocoder. You can check if Geocoder is implemented by the manufactures of the device with Geocoder.isPresent() but the method is unreliable. We can't ensure the Geocoder is implemented wit it.

    For custom Geocoder, you can use the following class which the usage is exactly like using the Geocoder:

    import android.location.Address;
    import android.util.Log;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class MyGeocoder {
    
      public static final String TAG = MyGeocoder.class.getSimpleName();
    
      static OkHttpClient client = new OkHttpClient();
    
      public static List<Address> getFromLocation(double lat, double lng, int maxResult) {
    
        String address = String.format(Locale.US,
            "https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language="
                + Locale.getDefault().getCountry(), lat, lng);
        Log.d(TAG, "address = " + address);
        Log.d(TAG, "Locale.getDefault().getCountry() = " + Locale.getDefault().getCountry());
    
        return getAddress(address, maxResult);
    
      }
    
      public static List<Address> getFromLocationName(String locationName, int maxResults)  {
    
        String address = null;
        try {
          address = "https://maps.google.com/maps/api/geocode/json?address=" + URLEncoder.encode(locationName,
              "UTF-8") + "&ka&sensor=false";
          return getAddress(address, maxResults);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        return null;
      }
    
      private static List<Address> getAddress(String url, int maxResult) {
        List<Address> retList = null;
    
        Request request = new Request.Builder().url(url)
            .header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .build();
        try {
          Response response = client.newCall(request).execute();
          String responseStr = response.body().string();
          JSONObject jsonObject = new JSONObject(responseStr);
    
          retList = new ArrayList<Address>();
    
          if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
            JSONArray results = jsonObject.getJSONArray("results");
            if (results.length() > 0) {
              for (int i = 0; i < results.length() && i < maxResult; i++) {
                JSONObject result = results.getJSONObject(i);
                Address addr = new Address(Locale.getDefault());
    
                JSONArray components = result.getJSONArray("address_components");
                String streetNumber = "";
                String route = "";
                for (int a = 0; a < components.length(); a++) {
                  JSONObject component = components.getJSONObject(a);
                  JSONArray types = component.getJSONArray("types");
                  for (int j = 0; j < types.length(); j++) {
                    String type = types.getString(j);
                    if (type.equals("locality")) {
                      addr.setLocality(component.getString("long_name"));
                    } else if (type.equals("street_number")) {
                      streetNumber = component.getString("long_name");
                    } else if (type.equals("route")) {
                      route = component.getString("long_name");
                    }
                  }
                }
                addr.setAddressLine(0, route + " " + streetNumber);
    
                addr.setLatitude(
                    result.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
                addr.setLongitude(
                    result.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
                retList.add(addr);
              }
            }
          }
        } catch (IOException e) {
          Log.e(TAG, "Error calling Google geocode webservice.", e);
        } catch (JSONException e) {
          Log.e(TAG, "Error parsing Google geocode webservice response.", e);
        }
    
        return retList;
      }
    }
    
    0 讨论(0)
  • 2021-01-21 14:19

    Geocoder is not implemented by all manufacturers. It's may be a reason why it doesn't work on a particular device. From the Geocoder documentation :

    The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform

    Or perhaps there is a request quota.

    Instead you can use a remote service like Google Maps geocoding API (quotas too).

    0 讨论(0)
提交回复
热议问题