没有从地理编码器获得正确的返回值?

发布于 2025-01-07 23:45:15 字数 1133 浏览 0 评论 0原文

我有一系列位置,并且使用地理编码器,我能够获得纬度和位置。经度。但是,我想每次从地理编码器函数中传递位置值。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2025-01-14 23:45:15

原因是地理编码器的响应是异步的。 for 循环遍历每个元素,并且由于“Jaipur”是最后一个值,因此当地理编码器的响应最终到达并调用 createMarker 时,该值仍然存储在 tempLoc 中

createMarker(latLonArray[i],tempLoc);   
// by this time tempLoc always equals tempLoc=locations[locations.length-1];

:要做的就是获取地理编码器返回的名称

results[0].address_components[0].long_name

因此您对 createMarker 的调用将如下所示:

createMarker(latLonArray[i], results[0].address_components[0].long_name );

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:

createMarker(latLonArray[i],tempLoc);   
// by this time tempLoc always equals tempLoc=locations[locations.length-1];

What you actually want to do is get the name back that geocoder returned

results[0].address_components[0].long_name

So your call to createMarker will look like:

createMarker(latLonArray[i], results[0].address_components[0].long_name );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文