I'm trying to get the latitude of a city using this function:
function get_lat(city) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": city
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
var location = results[0].geometry.location;
return location.lat();
} else {
}
});
}
This function (example: get_lat("Amsterdam")
always returns undefined
). The geocoder itself does work: adding console.log(location.lat())
before the return
line outputs the correct latitude.
Does anyone know what I am doing wrong?
Update:
How would I use the latitude in the map?
get_lat('Amsterdam', function(lat) {
$scope.map = {center: {latitude: lat, longitude: -99.6680 }, zoom: 4 };
});
doesn't work on first visit (it's in an Ionic app). After refreshing it does work.