2016-09-21 68 views
0

当我点击地图页面上的链接(不是自己标记它)时,我需要打开地图上的信息窗口。这是到目前为止我的代码,在google maps中通过marker id获取marker customInfo

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); 

     }); 
} 

在我的情况,我不能用一个数组来存储标记。有没有办法通过使用下面的条件获得marker.customInfo?请注意当我点击标记它的作品。我需要它用于后者的onclick功能。

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

请有人可以帮我 – cmit

回答

0

这可能会实现,保持你的bindInfoWindow里面,你现在有它:

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

通知我传递的作为数据参数点击事件句柄,但你也许不需要那样做。 - 看起来不需要。

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

我假设您要添加到每个标记的id属性与您在链接上阅读的“id”属性相同。如果没有,你可以在标记和链接上有一些相同的属性,你可以检查。

+0

谢谢老兄!你很棒。我无法相信这一点。爱你的男人 – cmit