How can I return the name of the city to show on html page?
My function
function getCity(latitude, longitude){
var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);
geocoder.geocode(
{'latLng': latlng},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
alert("city name is: " + city);
return city;
}
}
}
);
The alert is working fine.
In HTML I called this function like that:
<script> getCity("<%= map.latitude %>","<%= map.longitude %>")</script>
Tks in advance.