这里是Jsfiddle,它向您展示了如何使用标记进行地图 JavaScript 交互的“外部”。是的,您确实需要保存标记,并且它是数组中适当的 InfoWindow,以便您可以访问它。我创建了两个随机标记并使用数组中的坐标。
这里是 HTML 和两个通用链接,其中单击链接一个会以制造商 1 为中心并弹出其信息窗口,对于标记 2,反之亦然:
<div id='map_canvas'></div>
<a href='#' onClick="gotoPoint(1);">Click for marker 1</a><br/>
<a href='#' onClick="gotoPoint(2);">Click for marker 2</a>
在 createMarker 中,我将创建的 maker 与其关联的 InfoWindow 一起存储,并将其推送到全局范围的标记数组中。悬停标记时,它将打开其关联信息窗口:
function createMarker(lat, lon, html) {
var newmarker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
title: html
});
newmarker['infowindow'] = new google.maps.InfoWindow({
content: html
});
google.maps.event.addListener(newmarker, 'mouseover', function() {
this['infowindow'].open(map, this);
});
marker.push(newmarker);
}
在 gotoPoint 中,我只是简单地将标记编号作为参数输入。我基本上通过使用new google.maps.LatLng 将地图的中心设置为标记的中心,并通过调用marker[myPoint-1].position.lat(); 和marker[myPoint-1].position.lng(); 使用标记的纬度和经度,并使用marker[myPoint-1]['infowindow'].open(map, marker[myPoint-1]); 打开其关联的InfoWindow:
function gotoPoint(myPoint){
map.setCenter(new google.maps.LatLng(marker[myPoint-1].position.lat(), marker[myPoint-1].position.lng()));
marker[myPoint-1]['infowindow'].open(map, marker[myPoint-1]);
}
如果您对此示例有任何疑问,请告诉我。