问题
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