在谷歌地图中通过标记id获取标记customInfo

Getting marker customInfo by marker id in google maps

本文关键字:id 获取 customInfo 谷歌地图      更新时间:2023-09-26

我需要在地图上打开信息窗口,当我点击地图页面上的链接(不标记它自己)。下面是目前为止的代码,

var marker = new MarkerWithLabel({
                map: resultsMap,
                id: label,
                position: latlng,
                title: "Address",
                //  radius: int_radius ,
                draggable: false,
                labelAnchor: new google.maps.Point(10, 35),
                labelContent: label,
                labelClass: "labels",
                labelInBackground: false,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                icon: image,
                customInfo: "dynamic data for each marker"
        }); 

和调用函数

function bindInfoWindow(resultsMap, marker, infoWindow) {
    google.maps.event.addListener(marker, 'click',
        function(){
            infoWindow.setContent(marker.customInfo);
            infoWindow.open(resultsMap,marker);
        });
        $(document).on('click','.store-title', function(){ 
            var linkId = $(this).attr('id'); 
            infoWindow.setContent(marker.customInfo);
            infoWindow.open(resultsMap,marker);
        });
} 

在我的情况下,我不能使用数组来存储标记。有办法得到标记吗?使用如下的条件customInfo ?请注意,当我点击标记,它的工作。我需要它用于后面的onclick功能。

infoWindow.setContent(marker.customInfo where marker.id==linkId);

这可能会起作用,将其保留在您当前拥有的bindInfoWindow中:

$(document).on('click','.store-title', function() { 
    var linkId = $(this).attr('id');
    if (linkId == marker.id) {
        google.maps.event.trigger(marker, 'click');
    }
});

请注意,我将marker作为数据参数传递给单击事件句柄,但您可能不需要这样做。 -似乎不需要。

然后触发该标记上的点击,而不是重复你已经在标记的点击处理程序中得到的代码。

我假设你添加到每个标记的id属性与你在链接上阅读的"id"属性相同。如果没有,你能在标记和链接上有一些相同的属性吗?