簡體   English   中英

如何在具有兩個位置的Google地圖上添加標記?

[英]How to add marker on google map with two locations?

我想在Google地圖上添加具有兩個可點擊位置的標記。 當我單擊按鈕時,應該隨位置更改地圖標記。

 <script>
    var map;
    function initialize()
    {
        map = new google.maps.Map(document.getElementById('map-canvas'), {
            center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
            zoom: 14,

        });
        var marker = new google.maps.Marker({
            position: newLocation(),
            map: map,
            title: 'AGM-CORP',
            icon: 'img/agm-marker.png'
        });
    }

    function newLocation(newLat, newLng)
    {
        map.setCenter({
            lat: newLat,
            lng: newLng
        });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
    $(document).ready(function ()
    {
        $("#1").on('click', function ()
        {
            newLocation(52.302516, 16.414546);
        });

        $("#2").on('click', function ()
        {
            newLocation(51.706478, 15.274753);
        });
    });
</script>
<div id="map-canvas"></div>
</div>
    <h3>Zobacz lokalizację:</h3>
    <button id="1" style="padding:10px; cursor:pointer;">Siedziba Firmy</button>
    <button id="2" style="padding:10px;cursor:pointer;">Kopalnia Kruszyw</button>
</div>

此功能只會切換視圖的位置:

map.setCenter({
   lat: newLat,
   lng: newLng
});

改用這個:

new google.maps.LatLng(-25.363882,131.044922);

其次,您需要向標記對象添加事件列表器,以使其正常工作:

// create markers on the map
marker1 = new google.maps.Marker({ ... })
marker2 = new google.maps.Marker({ ... })
marker3 = new google.maps.Marker({ ... })

// add event listener
marker1.addListener('click', function() { ... })
marker2.addListener('click', function() { ... })

進一步查看文檔,它們非常簡單。

var map = null
var marker = null;
var myLatLng = {
  lat: 52.302516,
  lng: 16.414546
};

function initMap() {

  map = new google.maps.Map(document.getElementById('map'), {
    center: new google.maps.LatLng(myLatLng.lat, myLatLng.lng),
    zoom: 14,
  });

  marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    title: 'Hello World!'
  });
}

function updateMap(lat, lng) {;
  myLatLng.lat = lat;
  myLatLng.lng = lng
  map.setCenter(myLatLng);
  marker.setPosition(myLatLng);
}


$(document).ready(function() {
  $("#1").on('click', function() {
    updateMap(52.302516, 16.414546);
  });

  $("#2").on('click', function() {
    updateMap(51.706478, 15.274753);
  });
});

我正在用新標記初始化地圖。 工作jffiddle在這里

這將起作用。 有一個保存marker的全局變量。 無論何時更改位置,都可以使用setPosition方法將標記設置為LatLng的位置,並使用setCenter方法在中心顯示標記。 無需每次都初始化地圖。

var map,marker;
function initialize()
{
    map = new google.maps.Map(document.getElementById('googleMap'), {
        center: new google.maps.LatLng(52.302516, 16.414546), //Setting Initial Position
        zoom: 6,
    });
    setLocation(52.302516,16.414546);
}

function setLocation(newLat, newLng)
{
    var latlng = new google.maps.LatLng(newLat,newLng);
    if(marker) //Remove the marker, if already set
    {
        marker.setMap(null);
    }
    marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'AGM-CORP'
    });
    map.setCenter(latlng);

}

$(document).ready(function ()
{
    $("#1").on('click', function ()
    {
        setLocation(13.070814558816117, 80.2656996835234);
    });

    $("#2").on('click', function ()
    {
        setLocation(59.4739316352335,-110.89646088128342);
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM