我有一个严重的问题,我已经尝试调试了几天。我有一个脚本可以获取用户当前的纬度和经度,然后将它们存储在变量中。但是,当我尝试在此函数之外和 //init 地图区域中使用这些变量时,地图只是没有显示出来。通过提醒变量,我可以看到位置函数之外的变量设置为“未定义”。这是我的代码:
//main function here
function initialize() {
var lat;
var lon;
//check if user has geo feature
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(
//get position
function(position){
lat = position.coords.latitude;
lon = position.coords.longitude;
},
// if there was an error
function(error){
alert('ouch');
});
}
//case the users browser doesn't support geolocations
else {
alert("Your browser doesn't support geolocations, please consider downloading Google Chrome");
}
//init map
var myOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
感谢您的帮助,爱丽儿