没有从地理编码器获得正确的返回值?
我有一系列位置,并且使用地理编码器,我能够获得纬度和位置。经度。但是,我想每次从地理编码器函数中传递位置值。
var locations=new Array("Delhi","Jaipur")
for(var i=0;i<locations.length;i++){
var tempLoc=locations[i];
geocoder.geocode( { 'address': tempLoc},function(results, status)
{
if (status == google.maps.GeocoderStatus.OK) {
latitude[i] = results[0].geometry.location.lat();
longitude[i] = results[0].geometry.location.lng();
latLonArray[i]=new google.maps.LatLng(latitude[i],longitude[i]);
latlngbounds.extend( latLonArray[ i ] );
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
createMarker(latLonArray[i],tempLoc);
}
});
}
function createMarker(pos,t){
var marker = new google.maps.Marker({
position: pos,
map: map,
title: t
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(marker.title);
infowindow.open(map, marker);
});
return marker;
}
位置标记完美,但是当调用单击事件时,信息窗口不会根据位置显示(对于每个标记,信息窗口将标题显示为最后一个位置[“斋浦尔”])。
I've an array of locations and, using geocoder, I was able to get the latitude & longitude. However, I want to pass the location value each time out of the geocoder function.
var locations=new Array("Delhi","Jaipur")
for(var i=0;i<locations.length;i++){
var tempLoc=locations[i];
geocoder.geocode( { 'address': tempLoc},function(results, status)
{
if (status == google.maps.GeocoderStatus.OK) {
latitude[i] = results[0].geometry.location.lat();
longitude[i] = results[0].geometry.location.lng();
latLonArray[i]=new google.maps.LatLng(latitude[i],longitude[i]);
latlngbounds.extend( latLonArray[ i ] );
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
createMarker(latLonArray[i],tempLoc);
}
});
}
function createMarker(pos,t){
var marker = new google.maps.Marker({
position: pos,
map: map,
title: t
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(marker.title);
infowindow.open(map, marker);
});
return marker;
}
The locations are marking perfectly, but when a click event is called the info window is not showing according to the location (for every marker the info window shows the title as last location["jaipur"]).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是地理编码器的响应是异步的。 for 循环遍历每个元素,并且由于“Jaipur”是最后一个值,因此当地理编码器的响应最终到达并调用 createMarker 时,该值仍然存储在 tempLoc 中
:要做的就是获取地理编码器返回的名称
因此您对 createMarker 的调用将如下所示:
The reason is because the response from geocoder is asynchronous. The
for
loop goes through each element, and since 'Jaipur' is the last value, that is the value still stored in tempLoc when the response from geocoder finally comes in and calls createMarker:What you actually want to do is get the name back that geocoder returned
So your call to createMarker will look like: