谷歌地图信息窗口在不同的标记上给出相同的结果
Google maps info window give same result on different markers
有以下问题,信息窗口在不同的标记上给出相同的结果。当我在geocoder.geocode函数外部调试时,它会显示正确的信息,但内部总是相同的(第一个)。suppid部分是当我调试内部的i变量时,它会显示正确的值。
var optionMap = {
zoom: 16,
MapTypeId: google.maps.MapTypeId.ROADMAP,
panControl: false,
zoomControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false
};
var map = new google.maps.Map(document.getElementById('map'), optionMap);
var geocoder = new google.maps.Geocoder();
var latlngbounds = new google.maps.LatLngBounds();
var icon = new google.maps.MarkerImage('images/gm_pin.png',
new google.maps.Size(20, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
for(var i = 0; i < arrAddress.length; i++) {
var address = arrAddress[i];
var html_title = "<div class='info'><h4>" + address + "</h4></div>";
geocoder.geocode({
'address': address
}, function (results, status) {
if(status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
icon: icon,
html: html_title,
position: results[0].geometry.location
});
var contentString = '';
var infoWindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'mouseover', function() {
infoWindow.setContent(this.html);
infoWindow.open(map, this);
});
google.maps.event.addListener(marker, 'mouseout', function() {
infoWindow.close(map, this);
});
latlngbounds.extend(results[0].geometry.location);
if(i == arrAddress.length) {
map.fitBounds(latlngbounds);
}
google.maps.event.trigger(map, 'resize')
}
});
}
对Geocoder.geocode(request, callback)
的调用是异步的。当服务器响应时,会调用回调函数。通常此时,for循环已经完成。这可以得到您所描述的结果,例如,每个标记都有相同的内容,因为标记是在回调中创建的,这发生在循环结束后,因此它将使用html_title
中的最后一个值。
解决这个问题的方法是在循环中创建一个闭包,它将在循环中"捕获"变量html_title
。这样,回调将使用正确的html_title
值。
JavaScript中一个典型的显式闭包如下:
// Wrap a function in () and immediately call it with another ()
// Pass in the arguments in the second ()
// This causes them to be 'captured' inside the function
(function(args) { /* Do something with args */ })("Hello", 42);
在您的代码中,闭包需要在循环期间捕获一个地址:
for(var i = 0; i < arrAddress.length; i++) {
var address = arrAddress[i];
var html_title = "<div class='info'><h4>" + address + "</h4></div>";
(function(address, title) {
// Inside the closure, we can use address and html_title
geocoder.geocode({'address': address /* captured */}, function (results, status) {
if(status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
icon: icon,
html: title, /* captured */
position: results[0].geometry.location
});
var contentString = '';
var infoWindow = new google.maps.InfoWindow({
content: contentString
});
// ...
}
});
})(address, html_title); // <- Call function, pass the vars to be captured
}
相关文章:
- 为自定义网站创建JavaScript搜索框,创建显示搜索结果的弹出窗口
- 防止多次显示弹出窗口.Ajax添加结果已在DOM中注册
- 使用HTML搜索,需要JavaScript来搜索数组并在新窗口中返回结果
- 无法在新窗口中打开 javascript 搜索引擎结果
- 如何在带有复选框按钮的 HTML 弹出窗口中显示结果
- 如何使用 ajax 在 showModelDialog 中提交表单后在父窗口中显示结果
- 打开对话框,其中包含在使用 magnificPopup 构建的弹出窗口中跟随 URL 返回的结果
- 在jQuery窗口外使用FileReader中的结果值
- 使用AJAX结果动态更新引导弹出窗口
- 从弹出窗口中使用php和javascript传递多个MySQL结果集
- 当搜索没有结果时抛出弹出窗口
- 通过弹出窗口注册并在此弹出窗口中传递结果
- 谷歌地图信息窗口在不同的标记上给出相同的结果
- 点击弹出窗口中的按钮搜索结果
- 模态对话框返回值未定义-将结果发送到父窗口
- 在新选项卡或窗口显示表单提交的结果
- 从弹出窗口返回结果
- 使用窗口.反复打开会产生意想不到的结果
- 结合_.记住和_.节流以限制函数调用,并在一个时间窗口内缓存结果
- 无法在弹出式模式窗口中显示SQL结果