gpt4 book ai didi

google-maps-api-3 - Google Maps API v3-将更多信息传递给GeoCode回调吗?

转载 作者:行者123 更新时间:2023-12-03 14:47:16 25 4
gpt4 key购买 nike

我正在使用Google Maps API v3对地址进行地理编码。如何将其他信息传递给geocodeCallBack函数?请参阅下面的代码和注释,以了解我要实现的目标。

var address = new Array();
address[0] = {name:'Building 1',address:'1 Smith Street'};
address[1] = {name:'Building 2',address:'2 Smith Street'};

for(var rownum=0; rownum<=address.length; rownum++)
{
if(address[rownum])
geocoder.geocode( {'address': address[rownum].address}, geocodeCallBack);
}

function geocodeCallBack(results, status)
{
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
//how can I also add the name of the building to the title attribute?
title: results[0].formatted_address
});
}

最佳答案

关闭makeCallbackgeocodeCallBack。闭包获取并保留rownum

for (var rownum=0; rownum<=address.length; rownum++) {
if (address[rownum])
geocoder.geocode( {'address': address[rownum].address}, makeCallback(rownum));
}

function makeCallback(addressIndex) {
var geocodeCallBack = function(results, status) {
var i = addressIndex;
alert(address[i].name + " " + results[0].formatted_address);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
// use address[i].name
title: results[0].formatted_address
});
}
return geocodeCallBack;
}


当然,您也可以使用闭包 makeCallback(addressName)并直接传递名称,但是带有地址索引的上述版本更为通用。

关于google-maps-api-3 - Google Maps API v3-将更多信息传递给GeoCode回调吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7002973/

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