繁体   English   中英

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

[英]google maps - open marker infowindow given the coordinates

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

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

这是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>

在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);
}

在gotoPoint中,我只需将标记号作为参数。 我基本上通过使用new google.maps.LatLng将地图的中心设置为标记的中心,并通过调用marker[myPoint-1].position.lat();使用标记的lat和lng marker[myPoint-1].position.lat(); marker[myPoint-1].position.lng(); ,并使用marker[myPoint-1]['infowindow'].open(map, marker[myPoint-1]);打开其关联InfoWindow 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]);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM