谷歌地图 - 给定坐标打开标记infowindow

rak*_*los 4 javascript google-maps

我正在使用谷歌地图api v3.

使用javascript,我怎样才能打开标记的infowindow给出它的坐标?

KJY*_*葉家仁 11

这是Jsfiddle,它向您展示如何使用Markers进行地图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>
Run Code Online (Sandbox Code Playgroud)

在createMarker中,我将创建的制造商与其关联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);
}
Run Code Online (Sandbox Code Playgroud)

在gotoPoint中,我只需将标记号作为参数.我基本上是用设置地图到标记的中心new google.maps.LatLng,并通过调用使用标志的纬度和经度marker[myPoint-1].position.lat();marker[myPoint-1].position.lng();,并用打开其关联信息窗口marker[myPoint-1]['infowindow'].open(map, marker[myPoint-1]);:

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]);
}
Run Code Online (Sandbox Code Playgroud)

如果您对此示例有任何疑问,请与我们联系.