点击标记时,地理编码器返回之前存储的值 [英] geocoder is returning previously stored value when click on a marker
问题描述
我使用google map api v3(javascript)实施地理编码器。我正在放置多个标记,当我点击标记时,我想要相应的地址进入信息窗口。但是当我点击标记时,最初地址的默认值(按代码地理地址不可用!)即将到来,当我点击第二个标记时,它具有先前点击的标记的地址。
I'm implementing geocoder using google map api v3 (javascript). I'm putting mutiple markers and when i click on marker i want the corresponding address to come in the info-window. But when i click on the marker, initially the default value of address(As per code Geo-Address Unavailable!) is coming and when i click the second marker it is having the address of the previously clicked marker. I'm attaching the piece of code.
var addr=" Geo-Address Unavailable!";
var geocoder = new google.maps.Geocoder();
for(var i = 0; i < latLngs.length-1; i++){ //latLngs is the array of latlongs
marker = new google.maps.Marker({
position: latlng,
id:markerId,
icon: image,
map: map,
animation : google.maps.Animation.DROP,
});
}
markers[markerId] = marker;
var infowindow = new google.maps.InfoWindow({
content: "loading..."
});
google.maps.event.addListener(marker, "click", function () {
geocoder.geocode({'latLng': this.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
addr = results[0].formatted_address;
}
});
infowindow.setContent(addr);
infowindow.open(map, this);
});
推荐答案
地理编码是异步的。您必须在地理编码器的回调中使用返回的值。
Geocoding is asynchronous. You have to use the returned value inside the callback from the geocoder.
发生了什么:
What is happening:
- 您向地理编码器发送请求。
- 显示响应
- 响应从地理编码器返回 >
- 您向地理编码器发送另一个请求
- 显示第一个请求的响应
- 响应返回从地理编码器到第二个请求
- you send off a request to the geocoder.
- you display the response
- the response comes back from the geocoder
- you send off another request to the geocoder
- you display the response to the first request
- the response comes back from the geocoder to the second request
这篇关于点击标记时,地理编码器返回之前存储的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!