Using the Google Maps Geocoding API, i'm able to get the formatted address for a particular coordinate. To get the exact city name, I'm doing the following:
$.ajax({
url: 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=false',
success: function(data){
var formatted = data.results;
var address_array = formatted[6].formatted_address.split(',');
var city = address_array[0];
}
});
where lat
and long
are derived using the browser coordinates. My problem is the following:
From coordinates 19.2100
and 72.1800
, I get the city as Mumbai
, but from a similar set of coordinates about 3Km away, I get city as Mumbai Suburban
. How can I get Mumbai
without changing the success function of my code? It seems to me that the result array doesn't always stick to the same format which creates problems in my displaying of the city name.