簡體   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