我正在检索不同的数据以使用不同的 JSON 文件标记传单 map 。每个单选按钮检索一个不同的 JSON 文件。但是,当我选择不同的单选按钮时,我无法清除标记。所有标记只是从一个 JSON 文件添加到另一个文件。当我选择不同的单选按钮时,我希望能够清除所有标记。
我四处搜寻并阅读了 map.removeLayer(MyLayer);将删除所有标记。所以我创建了一个名为“markers”的标记数组,并放置在一个名为“markersLayer”的图层中。当我尝试删除“markersLayer”时,它没有在 map 上留下一个标记。现在没有从不同的 JSON 文件中清除以前的标记,而是没有绘制任何内容。
我只想使用我使用单选按钮选择的特定 JSON 文件中的数据来显示这些标记。
HTML:
<div style="text-align: center;">
<h1 id="title">Map Visualization 3</h1>
<label><input type="radio" class="location" name="location" value="locations1" checked="checked">Location Set 1</label>
<label><input type="radio" class="location" name="location" value="locations2">Location Set 2</label>
<label><input type="radio" class="location" name="location" value="locations3">Location Set 3</label>
<ul id="location-list"></ul>
<div id="map" style="width: 80%; max-width: 900px; height: 600px; margin: 0 auto;"></div>
</div>
JS:
var map;
var markers = [];
var markersLayer;
var updateMap = function() {
var uri = $('input.location:checked').val() + '.json';
$.getJSON(uri, function(response){
$('ul#location-list').empty();
var locationCoor = [];
var marker;
for(var i=0; i < response.length; i++){
var lat = response[i].latitude;
var lon = response[i].longitude;
$('ul#location-list').append('<li>(' + lat + ', ' + lon + ')</li>');
//console.log(lat, lon);
locationCoor[i] = [lat, lon];
//console.log(locationCoor);
var popup = L.popup()
.setLatLng([lat, lon])
.setContent('<h3 style="margin:0 0 3px 0;"><a href="' + response[i].link + '">' + response[i].title + '</a></h3><img width="180px" height="auto" src="' + response[i].imageUrl + '">');
marker = L.marker([lat, lon], {
clickable: true
}).bindPopup(popup, {showOnMouseOver:true});
markers[i] = marker;
console.log(markers);
}
markersLayer = L.layerGroup(markers);
markersLayer.addTo(map);
var bounds = new L.latLngBounds(locationCoor);
map.fitBounds(bounds, {padding: [50,50]});
markers.length = 0;
});
};
$(document).ready(function(){
map = L.map('map');
L.tileLayer('https://{s}.tiles.mapbox.com/v3/examples.map-i87786ca/{z}/{x}/{y}.png', {
maxZoom: 18,
id: 'examples.map-20v6611k'
}).addTo(map);
$('input.location').on('change', updateMap);
updateMap();
});
JSON 1:
[
{
"title": "Weathertop",
"link": "http://en.wikipedia.org/wiki/Weathertop",
"latitude": 38.80,
"longitude": -77.12,
"imageUrl": "assets/img/location-images/Weathertop.jpg"
},
{
"title": "Rivendell",
"link": "http://lotr.wikia.com/wiki/Rivendell",
"latitude": 38.78,
"longitude": -77.18,
"imageUrl": "assets/img/location-images/Rivendell2.jpg"
},
{
"title": "Minas Tirith",
"link": "http://lotr.wikia.com/wiki/Minas_Tirith",
"latitude": 38.76,
"longitude": -77.18,
"imageUrl": "assets/img/location-images/320px-Minas_Tirith.jpg"
}
]
JSON2:
[
{
"title": "Chicago",
"link": "http://en.wikipedia.org/wiki/Weathertop",
"latitude": 41.836,
"longitude": -87.604980,
"imageUrl": "assets/img/location-images/Weathertop.jpg"
},
{
"title": "Detroit",
"link": "http://lotr.wikia.com/wiki/Rivendell",
"latitude": 42.326062,
"longitude": -83.078613,
"imageUrl": "assets/img/location-images/Rivendell2.jpg"
},
{
"title": "Indianopolis",
"link": "http://lotr.wikia.com/wiki/Minas_Tirith",
"latitude": 39.741,
"longitude": -86.154785,
"imageUrl": "assets/img/location-images/320px-Minas_Tirith.jpg"
}
]
最佳答案
您不应该重新创建 markersLayer
目的。您想要的是创建一次,然后继续从中添加/删除标记。
在您定义 markersLayer
的行中在文件的顶部,您现在还想在此处将其定义为 L.LayerGroup。我们不会重新创建这个对象。
当您想要更新 map 时,您将清除 markersLayer
中的所有现有标记。 .这是通过调用 markersLayer.clearLayers()
来完成的。 .这不会删除 markersLayer
从 map 上。它只会删除该层包含的标记。
从该图层中删除所有标记后,您现在可以自由地将新图层添加到 markersLayer
.
您的代码将如下所示:
var map;
var markers = [];
var markersLayer = new L.LayerGroup(); // NOTE: Layer is created here!
var updateMap = function() {
// NOTE: The first thing we do here is clear the markers from the layer.
markersLayer.clearLayers();
var uri = $('input.location:checked').val() + '.json';
$.getJSON(uri, function(response){
$('ul#location-list').empty();
var locationCoor = [];
var marker;
for(var i=0; i < response.length; i++){
var lat = response[i].latitude;
var lon = response[i].longitude;
$('ul#location-list').append('<li>(' + lat + ', ' + lon + ')</li>');
//console.log(lat, lon);
locationCoor[i] = [lat, lon];
//console.log(locationCoor);
var popup = L.popup()
.setLatLng([lat, lon])
.setContent('<h3 style="margin:0 0 3px 0;"><a href="' + response[i].link + '">' + response[i].title + '</a></h3><img width="180px" height="auto" src="' + response[i].imageUrl + '">');
marker = L.marker([lat, lon], {
clickable: true
}).bindPopup(popup, {showOnMouseOver:true});
markersLayer.addLayer(marker);
console.log(markers);
}
// NOTE: We are no longer recreating the layer here. Remove these lines of code.
//markersLayer = L.layerGroup(markers);
//markersLayer.addTo(map);
var bounds = new L.latLngBounds(locationCoor);
map.fitBounds(bounds, {padding: [50,50]});
markers.length = 0;
});
};
$(document).ready(function(){
map = L.map('map');
L.tileLayer('https://{s}.tiles.mapbox.com/v3/examples.map-i87786ca/{z}/{x}/{y}.png', {
maxZoom: 18,
id: 'examples.map-20v6611k'
}).addTo(map);
// NOTE: We add the markersLayer to the map here. This way, the layer is only added once.
markersLayer.addTo(map);
$('input.location').on('change', updateMap);
updateMap();
});
关于json - 在调用另一个 JSON 之前删除传单 map 中的所有数据/标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318862/