it could find when i hit the search
Akbank Sanat istanbul
but when i use this code , it cant find:
private LatLng getCoors(String loc, String desc){
Log.v(TAG, "getCoors ");
loc = loc +" Istanbul";
if (Geocoder.isPresent()) {
Log.v(TAG, "getCoors geocoder present");
geocoder = new Geocoder(mContext, Locale.getDefault());
Log.v(TAG, "location: "+loc);
try {
From_geocode = geocoder.getFromLocationName(loc, 1);
Log.v(TAG, "fromgeocode: "+From_geocode);
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, "catched error - from geocode");//need to do something
}
if (!From_geocode.isEmpty()) {
Log.v(TAG, " getcoors fromgeocod enot not empty ");
coors = new LatLng(From_geocode.get(0).getLatitude(), From_geocode.get(0).getLongitude());
Log.v(TAG, "LATITUTE=====" + coors.latitude + " LONGITUTE=====" + coors.longitude);
return coors;
}//second if end
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
}//first if end
else{
Log.v(TAG, " hata getcoors geocoder not preset ");
}
return null;
}//getcoors end
when i use same address,
else{
Log.v(TAG, " hata getcoors fromgeocode empty ");
}
it goes here. SO, it means it cant fetch the address googlemaps can fetch.
What am i doing wrong? Is there easy way without using json or even not connecting?
Apparently the geocoder has issues accurately calculating locations in a single run. Try repeatedly asking for the location 5-10 times until a response is received.
Or, for a more accurate means of getting an address from a string, try implementing the following:
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;
}
And calling it like this:
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();
}
This code was from Shobhit Puri and the original question can be found here.