抱歉,如果已经回答了这个问题,我已经在StackOverflow和其他地方找了几个小时,无法解决。
我正在尝试使用Google Maps V3地理定位功能(Google Maps Javascript API V3 - Detecting the User's Location),并想在地图居中后添加一个简单的中心标记(除其他外)。
如果我在“ initialize”函数的末尾添加以下内容,则它似乎无权访问“ initialLocation”变量:
var marker = new google.maps.Marker({
position: initialLocation,
map: map
});
但是,如果我在此之前警告了initialLocation,那么它将起作用。
这是完整的代码,几乎与Google的示例相同:
var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag = new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Try W3C Geolocation method (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
contentString = "Location found using W3C standard";
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
} else if (google.gears) {
// Try Google Gears Geolocation
browserSupportFlag = true;
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
contentString = "Location found using Google Gears";
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
} else {
// Browser doesn't support Geolocation
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
//alert(initialLocation);
var marker = new google.maps.Marker({
position: initialLocation,
map: map
});
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
initialLocation = newyork;
contentString = "Error: The Geolocation service failed.";
} else {
initialLocation = siberia;
contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
}
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}
上面不会添加标记,但是如果您取消注释“ alert(initialLocation);”行,则它将起作用。
为什么是这样?我怀疑这是范围问题,但我不确定。
(对我的应用而言,重要的是在“ if(navigator.geolocation){”块之外可以访问initialLocation变量)
谢谢!
最佳答案
调用getCurrentPosition()是异步的,这意味着您确定位置后,将在调用中作为参数提供的function()将运行。因此,initialLocation不会立即设置,而是在调用这些回调函数后立即设置。
您需要做的是移动
var marker = new google.maps.Marker({
position: initialLocation,
map: map
});
在这样的功能里面。
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
contentString = "Location found using W3C standard";
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
var marker = new google.maps.Marker({
position: initialLocation,
map: map
});
}, function() {
handleNoGeolocation(browserSupportFlag);
});
正如您在示例中看到的那样,它们对那些回调函数之外的initialLocation不执行任何操作。