触发Google地图标记外部地图

时间:2013-05-31 10:06:08

标签: jquery google-maps

当我点击地图外的元素时,如何触发“infoWindow”?

我有这个:

var locations = [
  ['Location 1', 59.917224,10.587052, 1],
  ['Location 2', 59.912665,10.726153, 2]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 9,
  center: new google.maps.LatLng(59.913869,10.752245),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

拥有HTML:

<ul>
<li>Trigger Marker 1</li>
<li>Trigger Marker 1</li>
</ul>

有什么好主意吗?谢谢!

更新

在这里更新了代码:

var locations = [   ['地点1',59.917224,10.587052,1],   ['地点2',59.912665,10.726153,2] ];

map = new google.maps.Map(document.getElementById('map'), {
  zoom: 9,
  center: new google.maps.LatLng(59.913869,10.752245),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

var markerMap = {}

for (i = 0; i < locations.length; i++) {
  markerMap[ "marker-" + i ] = marker  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

 $(".marker-list").on('click', function() {
 var id = $(this).find('li').attr( 'data-id' );
 alert(id)
 infoWindow.open( map, markerMap[ id ] );
 }); 

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

HTML:

<ul class="marker-list">
<li data-id="marker-1">Trigger Marker 1</li>
<li data-id="marker-2">Trigger Marker 1</li>
</ul>

3 个答案:

答案 0 :(得分:2)

尝试使用循环内的任何id字段将标记放入对象,例如:

//before loop declare object
var markerMap = {}
...
//inside loop add marker to object with uuid (e.g mareker-1)
markerMap[ "marker-" + i ] = marker

然后将相同的id参数添加到HTML标记

<ul class="marker-list">
   <li data-id="marker-1">Trigger Marker 1</li>
   <li data-id="marker-2">Trigger Marker 2</li>
</ul>

最后将事件添加到li元素(例如使用jQuery)

 $(".marker-list").click( function() {
     var id = $(this).attr( 'data-id' );
     infoWindow.open( map, markerMap[ id ] );
 }); 

我没有测试这段代码。它是一个想法。我希望你知道我的想法

答案 1 :(得分:1)

根据您对代码的更新:

markerMap[ "marker-" + i ] = marker

这应该在“marker-1”上打开infowindow:

google.maps.event.trigger(markerMap["marker-1"], "click");

答案 2 :(得分:0)

将每个标记保存在一个数组中,然后在li上单击,只需使用数组中的对应标记

var markers = [];
for (i = 0; i < locations.length; i++) {  
  markers[i] = marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}