在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
当前回答
解决方法很简单。你可以使用方法:marker.setMap(map);。在这里,您定义大头针将出现在哪个映射上。
所以,如果你在这个方法中设置为null (marker.setMap(null);),大头针就会消失。
现在,你可以写一个函数女巫,而使所有的标记消失在你的地图。
您只需添加将您的大头针放入数组中并使用标记声明它们。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);
}
}
为了消除所有的标记,你应该调用null函数:
// 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 = [];
}
这是我的完整代码。这是我能化简的最简单的形式。 请注意,你可以用你的密钥谷歌API替换代码中的YOUR_API_KEY:
<!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>
您可以咨询谷歌开发人员或完整的文档,也,谷歌开发人员网站。
其他回答
我刚刚尝试了kmlLayer.setMap(null),它工作。不确定这是否适用于普通的标记,但似乎工作正确。
大多数投票的答案是正确的,但如果如果你只有一个标记(就像我曾在我的情况下),每次你需要杀死前面位置的标记,并添加一个新的然后你不需要创建整个数组的标记和管理每个推动和流行,您可以简单地创建一个变量来存储你的标记之前的位置,可以设置为null的创造新的。
//保存标记位置的全局变量。
var previousMarker;
//当添加新的标记时
if(previousMarker != null)
previousMarker.setMap(null);
var marker = new google.maps.Marker({map: resultsMap, position: new google.maps.LatLng(lat_, lang_)});
previousMarker = marker;
解决方法很简单。你可以使用方法:marker.setMap(map);。在这里,您定义大头针将出现在哪个映射上。
所以,如果你在这个方法中设置为null (marker.setMap(null);),大头针就会消失。
现在,你可以写一个函数女巫,而使所有的标记消失在你的地图。
您只需添加将您的大头针放入数组中并使用标记声明它们。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);
}
}
为了消除所有的标记,你应该调用null函数:
// 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 = [];
}
这是我的完整代码。这是我能化简的最简单的形式。 请注意,你可以用你的密钥谷歌API替换代码中的YOUR_API_KEY:
<!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>
您可以咨询谷歌开发人员或完整的文档,也,谷歌开发人员网站。
要从地图中删除所有标记,创建如下函数:
1.addMarker(location):这个函数用于在地图上添加标记
2.clearMarkers():该函数从map中移除所有标记,而不是数组中
3.setMapOnAll(map):这个函数用于在数组中添加标记信息
4.deleteMarkers():该函数通过删除对数组中所有标记的引用来删除它们。
// 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 = [];
}
在这里你可以找到一个如何删除标记的例子:
https://developers.google.com/maps/documentation/javascript/examples/marker-remove?hl=es
// Add 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 setAllMap(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() {
setAllMap(null);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}