Add marker to existing google map (without refresh google map) [duplicate]

筅森魡賤 提交于 2019-12-31 07:54:08

问题


I have a google map on my website (in production) with few markers, i would like to know if its possible to add a marker on an existing map without refresh my google map.

This what i have:

  • I load my webpage, google map appears with markers

  • On click on a button, i would like to add one marker on my map (without refresh my page or the google map)

I can realize this with refresh my google map but i think that its better to do this without refresh due to limitation of google map display per day.

This is my code:

    <button onclick="addMarker()">Add marker</button>
<script>
    function addMarker(){
    var myLatLng = new google.maps.LatLng(44, 6);
    var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
            });
    }
</script>

回答1:


var latlng = new google.maps.LatLng(42.745334, 12.738430);

function addmarker(latilongi) {
    var marker = new google.maps.Marker({
        position: latilongi,
        title: 'new marker',
        draggable: true,
        map: map
    });
    map.setCenter(marker.getPosition())
}

$('#btnaddmarker').on('click', function() {
    addmarker(latlng)
})



回答2:


Yes. When you added your markers originally, your probably did something like:

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

The map already existed (map: map), and you added markers. You can do the same thing later to add more markers.



来源:https://stackoverflow.com/questions/28706541/add-marker-to-existing-google-map-without-refresh-google-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!