Решение очень легко. Вы можете использовать метод: marker.setMap(map);
. Здесь вы определяете, на какой карте появится вывод.
Итак, если вы установили null
в этом методе (marker.setMap(null);
), штырь исчезнет.
Теперь вы можете написать функцию, которая, в то время как make, удаляет все маркеры на вашей карте!
Вы просто должны положить булавки в массив и объявить их markers.push (your_new pin)
или этот код, например:
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker);
}
Это функция, который может поставить или исчезнуть все маркеры вашего массива в карте:
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
исчезнуть все маркеры, вы должны вызвать функцию с нулем:
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
И, чтобы удалить и исчезают, все ваши маркеры, вы должны сбросить массив маркеров, как это:
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
Это мой полный код. Будьте уход полный вы можете заменить YOUR_API_KEY
в коде вашего ключа Google API:
<!DOCTYPE html>
<html>
<head>
<title>Remove Markers</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<p>Click on the map to add markers.</p>
<script>
// In the following example, markers appear when the user clicks on the map.
// The markers are stored in an array.
// The user can then click an option to hide, show or delete the markers.
var map;
var markers = [];
function initMap() {
var haightAshbury = {lat: 37.769, lng: -122.446};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: haightAshbury,
mapTypeId: 'terrain'
});
// This event listener will call addMarker() when the map is clicked.
map.addListener('click', function(event) {
addMarker(event.latLng);
});
// Adds a marker at the center of the map.
addMarker(haightAshbury);
}
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker);
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
Вы можете проконсультироваться google developer или полную документацию здесь, также, google developer web site.
Я нашел некоторый код по ссылке ниже, но святая корова - это много кода для имитации предыдущей 1 строки кода в версии 2 API. http://www.lootogo.com/googlemapsapi3/markerPlugin.html –
помните, что карты 3.0 должны быть ОЧЕНЬ легкими, чтобы мобильные устройства могли использовать его с минимальным запаздыванием ... – Petrogad
Добавлены профи/минусы. –