Google API 3 open Infowindow from divs outside google maps

若如初见. 提交于 2020-01-02 11:23:08

问题


As the title states how can I with Google API 3 open Infowindow from divs outside google maps.

this is what I am doing today but this only working on the last div link and not every link

markerObj =  document.getElementById(markerId);
                   markerObj.onclick = function(){
                       google.maps.event.trigger(marker, 'click');
                   } 

Any clues?


回答1:


I think to make this work you'd need to have an array of markers. Then you'd trigger the click on the exact marker you want (right now it'll just do it on your last marker).

So, as you add markers to your map, append them into an array:

var markers = [];

var marker = new google.maps.Marker({   ... });

marker.addListener('click', function() {
    infowindow.setContent('blah blah');
    infowindow.open(map, this);
});

markers.push(marker);

Then on your divs, pass in an ID to work out which of the array you're dealing with.

<div id="link0">1</div> <div id="link1">2</a> (JS arrays start at zero)

Then just a slight amend to your code:

markerObj =  document.getElementById(markerId);

// work out which number it is
var linkID = markerId.replace("link", "");

markerObj.onclick = function(){
    google.maps.event.trigger(markers[linkID], 'click');
}


来源:https://stackoverflow.com/questions/7555839/google-api-3-open-infowindow-from-divs-outside-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!