Google maps api 3 在谷歌地图的多个标记旁创建多个单独的信息窗口

Google maps api 3 在谷歌地图的多个标记旁创建多个单独的信息窗口,google-maps-api-3,xml-parsing,infowindow,Google Maps Api 3,Xml Parsing,Infowindow,我的项目搜索eBay API(使用PHP并返回simpleXML)并返回多个项目的邮政编码(目前为5个)。然后使用这些信息在我的网站上的谷歌地图上绘制标记。我想做的是创建多个信息窗口以及这些标记,这样我也可以返回易趣拍卖的信息,并将其放入信息窗口(拍卖链接、物品图片等),但我没有运气!我似乎无法在循环中获得正确的闭包,我一直在获取信息窗口中显示的数组中的最后一个邮政编码,而不是与该标记实际关联的邮政编码(只是为了测试目的而这样做) 我做错了什么?任何信息都会有帮助 这是我目前的代码: for (

我的项目搜索eBay API(使用PHP并返回simpleXML)并返回多个项目的邮政编码(目前为5个)。然后使用这些信息在我的网站上的谷歌地图上绘制标记。我想做的是创建多个信息窗口以及这些标记,这样我也可以返回易趣拍卖的信息,并将其放入信息窗口(拍卖链接、物品图片等),但我没有运气!我似乎无法在循环中获得正确的闭包,我一直在获取信息窗口中显示的数组中的最后一个邮政编码,而不是与该标记实际关联的邮政编码(只是为了测试目的而这样做)

我做错了什么?任何信息都会有帮助

这是我目前的代码:

for (var i = 0; i < msg.length; i++) {
    info = msg[i];
    console.log(info);
    geocoder.geocode( { 'address': msg[i]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,
                animation: google.maps.Animation.DROP,
                icon: image,
                position: results[0].geometry.location
            })
            listenMarker(marker);
            markerBounds.extend(results[0].geometry.location);
            map.fitBounds(markerBounds);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

function listenMarker (marker){
    google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(info);
    infoWindow.open(map, this);
});
for(变量i=0;i
您还需要在geocoder调用上使用函数闭包(未测试),看起来您的listMarker函数也可能有问题(似乎缺少“info”的定义,如果您依赖于该函数的全局值,这可能是您的问题):

功能地理编码地址(msg)
{
geocoder.geocode({'address':msg},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var marker=new google.maps.marker({
地图:地图,
动画:google.maps.animation.DROP,
图标:图像,
位置:结果[0]。几何体。位置
})
listenMarker(标记,味精);
markerBounds.extend(结果[0].geometry.location);
地图边界(markerBounds);
}否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
对于(变量i=0;i
谢谢!我这样做了,使用了一些摆弄之后的原理,效果非常好
function geocodeAddress(msg)
{
            geocoder.geocode( { 'address': msg}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
            map: map,
            animation: google.maps.Animation.DROP,
            icon: image,
            position: results[0].geometry.location
            })
                listenMarker(marker, msg);
                markerBounds.extend(results[0].geometry.location);
                map.fitBounds(markerBounds);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

for (var i = 0; i < msg.length; i++) {
            info = msg[i];
            console.log(info);
            geocodeAddress(msg[i]);
}

function listenMarker (marker, info){
    google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(info);
    infoWindow.open(map, this);
});