开发者

How to select and manipulate google maps (V3 JS API) marker by ID?

I got a Google Map with markers on the left side and a list with content items explaining those markers on the right side. When i click a marker, the list will scroll to the according entry.开发者_如何学运维 Thats works already. Now to visualize it better, i want to highlight the marker in the map when a list item in the right gets clicked.

I got it all working except that i don't know how to select a marker by it's id. I'm using this in a jquery. It works but only for the last created marker.

$(document).ready(function() {
  $('#list_canvas h2').click(function() {
    marker.setIcon('http://google-maps-icons.googlecode.com/files/black01.png');
  });
});

How can i change the line to select a marker by it's ID?


You should create a datastructure that you can use to bind the markers to their trigger elements. When you click an element, you could look up the corresponding marker and open it. E.g:

var markers = [];
$('li').each(function(){
  markers[$(this).index()] = new google.maps.Marker({ ... });
  $(this).click(function(){
    var marker = markers[$(this).index()];
    doStuffOnMarker(marker);
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜