0

I have created an array of markers on a map. And I have a list of these markers outside of the canvas. I would like to learn about and create some interaction between these.

My markers are created like so:

var markers = [];
var marker = [];
var i;  

// var places is an array of locations
for (var i=0;i<places.length;i++) {

        var place = places[i];
        var latLng = new google.maps.LatLng(place[1], place[2]);

         marker[i] = new google.maps.Marker({
            position: new google.maps.LatLng(place[1], place[2]),map: map
        });

        marker[i].set('id','marker_'+i);

        markers.push(marker[i]);

}

I thought perhaps by using MVCObject I could set the id of the marker, and perhaps my marker would have an id of 'marker_1' for example, which I could access in the DOM with jquery via something like:

$('#marker_0').doSomething();

But this doesn't seem to be the case.

How can I give each marker a unique Id to access in the DOM outside the map canvas and API?

4

1 回答 1

0

回答我的问题,这个答案在这里

我在谷歌地图的初始化函数中创建了一个函数 var:

var markers = [];
var marker = [];
var i;  

// var places is an array of locations
for (var i=0;i<places.length;i++) {

    var place = places[i];
    var latLng = new google.maps.LatLng(place[1], place[2]);

     marker[i] = new google.maps.Marker({
        position: new google.maps.LatLng(place[1], place[2]),map: map
    });

    marker[i].set('id','marker_'+i);

    markers.push(marker[i]);


    var mouseoverListener = (function(i) {
        return function() {

            alert[i];

        };
    })(i);

}

然后像这样启动addDomListener

     google.maps.event.addDomListener(document.getElementById('place'+i+''), 'mouseover', mouseoverListener);

当我将鼠标悬停在我的列表项上时,它会提醒它应该出现的标记的索引,因此我可以访问对每个标记的控制权,而不是 alert[i]。

于 2013-03-07T13:01:17.407 回答