首页 > 解决方案 > 将更多信息传递到 Google Maps InfoWindow

问题描述

我正在尝试将更多信息传递到 Google Maps 地图 InfoWindow,但不知何故它不起作用。这一定只是一个简单的愚蠢的事情,但我已经花了几个小时,但找不到它。

我的数据存储为:

res[i].id
res[i].name
res[i].date

等等

然后我输出它:

var marker = new google.maps.Marker({ position: new

google.maps.LatLng(res[i].position.lat,res[i].position.long),
                            title:'',
                            orderid:res[i].id,
                            map:map,
                            icon: icon,
                        });    

google.maps.event.addListener(marker, "click", (function(id) {
    return function() {
    var contentString = '<div><b>Order ID:</b> '+id+'
                         <br><b>Name:</b> '+res[i].name+'</div>'
                         <br><b>Date:</b> '+res[i].date+'</div>';
    infowindow.setContent(contentString);
    infowindow.open(map, this);
    }
})(res[i].id));

使用id时输出正常,id但我不知道如何传递其他变量。由于某种原因,名称和日期总是显示Cannot read property 'XXX' of undefined

我究竟做错了什么?注意这张地图上可能有很多标记,它是一个for循环。

标签: jquerygoogle-maps

解决方案


找到了解决方案:

google.maps.event.addListener(marker, 'click', (function(marker,i){
        return function(){
        infoWindow.setContent(infoWindowContent[i]);
        infoWindow.open(map, marker);
        }
})(marker,i));

function getInfoWindowDetails(location){
                            var contentString = '<div id="content" style="width:270px;height:100px">' +
                                                '<h3 id="firstHeading" class="firstHeading">' + location.name + '</h3>'+
                                                '<div id="bodyContent">'+
                                                    '<div style="float:left;width:100%">'+ location.status + '</div>'+
                                                '</div>'+
                                            '</div>';
                            return contentString;
                        }

希望这对某人有帮助!


推荐阅读