Skip to content Skip to sidebar Skip to footer

Geocoder Often Returns Null Vallues In Android

I am trying to pass geocoder values from MapFragment to LocationDetailsActivity. I get the correct values for lat and lng, but when I try to display any of the other values, most

Solution 1:

Android's geocoding api is pretty unreliable up-to my experience, I usually make a request to the Google's geocoding webservices on Url : "https://maps.googleapis.com/maps/api/geocode" (If you are familiar with retrofit)

@GET("/json")
    void reverseGeoCode(@Query("latlng") String latlng, @Query("language") String language,
                        @Query("key") String key, Callback<ReverseGeoCode> callback);

latlng The latitude and longitude you want to reverse geocode.

language language of the geocoded response

key Your Api key

Go here for more info

Solution 2:

Geocoder often got the values right, but more often than not I got null values. Based on @insomniac's advice I modified my code:

publicvoidonMapLongClick(final LatLng arg0) {

            RequestQueue queue = Volley.newRequestQueue(getActivity());
            String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKeyCode";

            // Request a string response from the provided URL.StringRequest stringRequest = newStringRequest(Request.Method.GET, url,
                    newResponse.Listener<String>() {
                        @OverridepublicvoidonResponse(String response) {
                            try {
                                JSONArray jObj = newJSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                                Intent intent = newIntent(getActivity(), LocationDetailsActivity .class);

                                for (int i = 0; i < jObj.length(); i++) {
                                    String componentName = newJSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                                    if (componentName.equals("postal_code") || componentName.equals("locality")) {
                                        intent.putExtra(componentName, newJSONObject(jObj.getString(i)).getString("short_name"));
                                    }
                                }

                                intent.putExtra("latitude", arg0.latitude);
                                intent.putExtra("longitude", arg0.longitude);

                                startActivity(intent);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, newResponse.ErrorListener() {
                @OverridepublicvoidonErrorResponse(VolleyError error) {
                    int x = 1;
                }
            });
    // Add the request to the RequestQueue.
            queue.add(stringRequest);

It still displays some areas as null. But those are smaller areas. Hope someone finds it helpful.

Post a Comment for "Geocoder Often Returns Null Vallues In Android"