最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google API 3 open Infowindow from divs outside google maps - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 10, 2013 at 7:42 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Sep 26, 2011 at 13:30 TobiasTobias 4943 gold badges14 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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');
}
发布评论

评论列表(0)

  1. 暂无评论