関数に渡された GPS 座標の町を返す関数を作成しましたが、何らかの理由で町が返されません。町に警告すると、正しい町が表示されます。
コード:
function getTown(latitude,longitude){
// Define Geocoding
var geocoder = new google.maps.Geocoder();
// Using the longitude / latitude get address details
var latlng = new google.maps.LatLng(latitude,longitude);
geocoder.geocode({'latLng': latlng}, function(results, status){
// If response ok then get details
if (status == google.maps.GeocoderStatus.OK) {
var town = results[1].address_components[1].long_name;
return town; // Returns Norwich when alerted using the e.g below.
}
});
}
例:
getTown(52.649334,1.288052);