gpt4 book ai didi

javascript - gmap3 在信息窗口中显示地址

转载 作者:行者123 更新时间:2023-11-30 05:57:01 27 4
gpt4 key购买 nike

我在我的 map 标记上有一个点击事件来创建一个信息窗口,我正在尝试对地址进行地理编码以显示在信息窗口内。

            click: function(marker) {
pantoUser(lati,longi,i);
addInfoWindow(lati,longi,name,datestring);
}

我几乎让它像这样工作:

function addInfoWindow(lati,longi,name,datestring)
{
// get address
getAddress(lati,longi);

// create infowindow
$('#dispatcher').gmap3(
{ action: 'addInfoWindow',
latLng: [lati, longi],
infowindow:{
options:{
content: name
},
events:{
closeclick: function(infowindow, event){
//alert('closing : ' + $(this).attr('id') + ' : ' + infowindow.getContent());
}
},
apply:[
{ action:'setContent',
args:[
'<span class="infowindow">' + name + '<br />' + content + '<br />' + datestring + '<span>'
]
}
]
}
}
);

然后是获取地址部分:

 function getAddress(lati,longi)
{
$("#dispatcher").gmap3({
action:'getAddress',
latLng: [lati, longi],
callback:function(results){
content = results && results[1] ? results && results[1].formatted_address : 'No Address';
return content;
}

});
}

问题是地理编码地址总是落后一个标记点​​击。例如,我可能点击伦敦的一个标记,但没有任何反应。所以我再次点击它,我得到了带有地址的信息窗口。然后我点击曼彻斯特的一个标记,我仍然看到伦敦地址,然后我点击利物浦的一个标记,我得到曼彻斯特的地址。等等。

谁能发现这个错误?

来自 SEAN 的更新工作解决方案在回调中添加信息窗口代码

function addInfoWindow(lati,longi,name,datestring)
{
// get address
$("#dispatcher").gmap3({
action:'getAddress',
latLng: [lati, longi],
callback:function(results){
content = results && results[1] ? results && results[1].formatted_address : 'No Address';

// create infowindow
$('#dispatcher').gmap3(
{ action: 'addInfoWindow',
latLng: [lati, longi],
infowindow:{
options:{
content: name
},
events:{
closeclick: function(infowindow, event){
//alert('closing : ' + $(this).attr('id') + ' : ' + infowindow.getContent());
}
},
apply:[
{ action:'setContent',
args:[
'<span class="infowindow">' + name + '<br />' + content + '<br />' + datestring + '<span>'
]
}
]
}
}
);

} // end callback

});

最佳答案

这看起来不像标准的 Google Maps JavaScript v3 代码; gmap3 是对 jQuery 的引用吗?此外,某些东西似乎没有卡在一起 - getAddress 函数返回 content,但是 addInfoWindow 函数中的代码似乎没有使用该值。

但听起来问题的根源可能在于 getAddress 函数中代码的 callback 方面;当 addInfoWindow 中该调用后面的代码运行时,您无法确定回调是否已完成。听起来可能会发生以下情况:

  1. addInfoWindow 函数被调用
  2. addInfoWindow 中调用 getAddress 函数
  3. getAddress 快速运行并完成,将控制返回给 addInfoWindow 函数,但回调尚未运行
  4. 代码第一次运行时,content还没有构建(因为回调还没有运行),所以什么都没有显示
  5. addInfoWindow 函数返回后,getAddress 回调运行,因为收到了响应,它建立了 content
  6. addInfoWindow 再次被调用,以相同的方式运行,但这次它使用了在上次回调之后设置的内容,所以它使用的是 content上次运行
  7. 场景只是重复,重复,重复

你的问题有一些令人困惑的部分,如果不能向代码添加 alert 调用或看到代码运行,很难确定,但听起来很可能是你的代码正在按照我上面描述的步骤或非常相似的步骤进行操作。

如果这听起来符合您的问题,解决它的最直接方法是将 InfoWindow 显示代码与回调代码放在一起。这样,您就知道内容已经设置好,因为回调会在响应返回时运行。

关于javascript - gmap3 在信息窗口中显示地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224883/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com