[google maps API] 清除所有 marker
- var markers = [];
- window.onload = function () {
- var mapOptions = {
- center: new google.maps.LatLng(21.0000, 78.0000),
- zoom: 5,
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
- //Attach click event handler to the map.
- google.maps.event.addListener(map, 'click', function (e) {
- //Determine the location where the user has clicked.
- var location = e.latLng;
- //Create a marker and placed it on the map.
- var marker = new google.maps.Marker({
- position: location,
- map: map
- });
- //Attach click event handler to the marker.
- google.maps.event.addListener(marker, "click", function (e) {
- var infoWindow = new google.maps.InfoWindow({
- content: 'Latitude: ' + location.lat() + '<br />Longitude: ' + location.lng()
- });
- infoWindow.open(map, marker);
- });
- //Add marker to the array.
- markers.push(marker);
- });
- };
- function DeleteMarkers() {
- //Loop through all the markers and remove
- for (var i = 0; i < markers.length; i++) {
- markers[i].setMap(null);
- }
- markers = [];
- };
主要就是把 marker 都存到陣列後再一次刪掉。
Source
留言