我使用這個腳本從一個地址獲取經緯度,如何從功能獲取經緯度並將其發送到新的google.maps.LatLng(經度,緯度)的JavaScript
var latitude="";
var longitude ="";
function codeAddress(address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == 'OK') {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
// myAfterFunction();
// console.log(this.latitude);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
var center = new google.maps.LatLng(latitude,longitude);
我的問題是我無法在這部函數來獲取經緯度值,我嘗試使用其他功能
function myAfterFunction(){
console.log(latitude);
}
所以我的問題是如何從我的功能得到緯度和經度?謝謝
你得到的錯誤是什麼? –
當我寫console.log(緯度); console.log(longitude);出了函數,它說undefined undefined –