Using the Google Maps geocoding API, I can get a formatted address for a specific coordinate. To get the exact name of the city, I do 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 latthey longare displayed using browser coordinates. My problem is this:
From the coordinates 19.2100and 72.1800, I get the city as Mumbai, but from a similar set of coordinates of about 3 km, I get the city as Mumbai Suburban. How can I get Mumbaiwithout changing the success function of my code? It seems to me that the array of results does not always adhere to the same format, which creates problems when displaying the name of the city.