我正在尝试在mapOptions中获取地理位置坐标,但是在我的if语句之后值不会保持设置。
var mapOptions;
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
mapOptions = {
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
zoom: 8
};
console.log(mapOptions); // shows correct geolocation
});
} else {
// San Francisco, supposed to be user coords
mapOptions = {
center: new google.maps.LatLng(37.77493, -122.419416),
zoom: 8
};
}
console.log(mapOptions); // is null
任何帮助将不胜感激,谢谢!