Android Geocoder Getfromlocationname Always Returns Null

Android Geocoder getFromLocationName always returns null

Geocoder doesn't always return a value. You can try to send a request 3 times in a for loop. I should be able to return atleast once. If not then, their might be a connection issue or can be other issues like server dis not reply to your request. Try and see these threads:

Geocoder doesn't always return a value and geocoder.getFromLocationName returns only null

Updated:

I had a while loop as well but I used to try it maximum for 10 times. Sometimes, it never returned anything even if it was connected t internet. Then, I used this much more reliable way to get the address everytime:

public JSONObject getLocationInfo() {

HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();

try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}

JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}

I called it as follows:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();

}

Hope this helps. I was also tired of relying on geocoder. This worked for me.
If you replace the URL with the lat and longitude coordinates and see the returned JSON object in a web browser. You'll see what just happened.

Geocoder often returns null vallues in Android

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

Geocoder.getFromLocation() does not return any addresses

From the android documentation:

The Geocoder class requires a backend service that is not included in
the core android framework.

The issue is Geocoder backend service is not available in your device/simulator. Your device/simulator needs Google API in order to works correctly with Grocoder.

getFromLocationName().size() returns always 0 - Android

A problem might be that the lookup is not done in a background thread, try and copy-paste the code from my answer here: Google Geocoder service is unavaliable (Coordinates to address)

Then run new GetAddressPositionTask().execute("someAddress") and see what happens.

Passing a context to the AsyncTask:

Add a constructor:

public class GetAddressPositionTask extends
AsyncTask<String, Integer, LatLng> {

private Context context;

public GetAddressPositionTask(Context context) {
this.context = context;
}

// .. rest of the code here

}

Usage:

GetAddressPositionTask(context).execute("someAddress")

Where context is either getApplicationContext() or getActivity().getApplicationContext() if called from fragment.

Regarding the nullpointer:

Not 100% clear where you are getting the nullpointer, but you should be able to do like this:

// in onPostExecute    
i.putParcelable("latlng", result);

and in MapsActivity

LatLng position = (getIntent() != null) ? getParcelable("latlng") : null;

Another place of concern is this.mainContxt. Simply do YourActivity.this instead or getApplicationContext(). If you are in a Fragment then do Context context = getActivity() and ensure that it is not null.



Related Topics



Leave a reply



Submit