javascript MapBox 清除所有当前标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46155523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MapBox clear all current markers
提问by HomerPlata
I've created a MapBox instance with:
我创建了一个 MapBox 实例:
var map = new mapboxgl.Map({
container : 'map',
style : 'mapbox://styles/mapbox/streets-v9'
});
I need to clear all markers, and have tried things like map.remove(marker) on each one, and a few other things, but nothing seems to work.
我需要清除所有标记,并在每个标记上尝试了诸如 map.remove(marker) 之类的东西,以及其他一些东西,但似乎没有任何效果。
Is there a simple function call to just clear all markers from the map?
是否有一个简单的函数调用来清除地图上的所有标记?
EDIT: Different from How to remove all layers and features from map?because I get "eachLayer is not a recognised function" (or similar) in console.
编辑:不同于如何从地图中删除所有图层和特征?因为我在控制台中得到“每一层不是公认的函数”(或类似的)。
回答by Andi-lo
You saw this? https://www.mapbox.com/mapbox-gl-js/api/#marker#remove
你看到了吗?https://www.mapbox.com/mapbox-gl-js/api/#marker#remove
Instead of map.remove maybe try marker.remove:
而不是 map.remove 可能尝试marker.remove:
var marker = new mapboxgl.Marker().addTo(map);
marker.remove();
回答by AIT MANSOUR Mohamed
If you added multiple markers, and you want to clear all them on your map, you have to loop overs all markers, and delete them one by one, you will have something like this :
如果您添加了多个标记,并且想要清除地图上的所有标记,则必须遍历所有标记,然后将它们一一删除,您将得到如下内容:
if (currentMarkers!==null) {
for (var i = currentMarkers.length - 1; i >= 0; i--) {
currentMarkers[i].remove();
}
}
Consider thar var currentMarkerscontains all markers, you can do this with sometning like :
考虑到tharvar currentMarkers包含所有标记,您可以使用以下方法执行此操作:
oneMarker= new mapboxgl.Marker(currentMarkerDiv)
.setLngLat(marker.geometry.coordinates)
.addTo(mapboxMap);
currentMarkers.push(oneMarker);
Where var currentMarkersis a global variable :
其中 var currentMarkers是一个全局变量:
var currentMarkers=[];
Full example :
完整示例:
// markers saved here
var currentMarkers=[];
// tmp marker
var oneMarker= new mapboxgl.Marker(currentMarkerDiv)
.setLngLat(marker.geometry.coordinates)
.addTo(mapboxMap);
// save tmp marker into currentMarkers
currentMarkers.push(oneMarker);
// remove markers
if (currentMarkers!==null) {
for (var i = currentMarkers.length - 1; i >= 0; i--) {
currentMarkers[i].remove();
}
}
回答by Ralph1983
If you added the markers like this (to be able to add styling and custom images as markers), then you can simply remove the class (I did it through Jquery though).
如果您添加了这样的标记(以便能够添加样式和自定义图像作为标记),那么您可以简单地删除该类(不过我是通过 Jquery 完成的)。
Adding markers that are in a GeoJson:
添加 GeoJson 中的标记:
GeoJson.features.forEach(function(marker) {
var el = document.createElement('div');
el.className = 'marker';
new mapboxgl.Marker(el).setLngLat(marker.geometry.coordinates).addTo(map);
});
Removing markers:
去除标记:
$( ".marker" ).remove();
$( ".marker" ).remove();

