gpt4 book ai didi

javascript - 如何在 Javascript 中使用传单 slider 和 markercluster?

转载 作者:搜寻专家 更新时间:2023-10-31 08:43:23 24 4
gpt4 key购买 nike

我正在制作一张使用 slider 显示或隐藏标记的 map ,我想添加聚类功能,每个单独的功能都能完美运行,但我希望 slider 显示标记,如果标记非常接近使用集群。问题是,个体和标记簇都在显示,我希望显示的标记成簇,而不是一直存在的簇

 <script type="text/javascript">
var sliderControl = null;
//creating layers
var cities = new L.LayerGroup();
var mbAttr = 'Map data &copy; <a href="http://openstreetmap.org">OpenstreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
mbUrl = 'https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png';
var grayscale = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {id: 'remote-sensing.n8k508ak', attribution: mbAttr, accessToken: 'pk.eyJ1IjoicmVtb3RlLXNlbnNpbmciLCJhIjoiYWNiYzg0ZWU2Mjk3ZTU5NjE4MmQyZWEzZTY2ZWNlYjIifQ.U7mp4MXdcjaIwW_syAqriQ'})
, streets = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {id: 'remote-sensing.84f6c85a', attribution: mbAttr, accessToken: 'pk.eyJ1IjoicmVtb3RlLXNlbnNpbmciLCJhIjoiYWNiYzg0ZWU2Mjk3ZTU5NjE4MmQyZWEzZTY2ZWNlYjIifQ.U7mp4MXdcjaIwW_syAqriQ'});
//create and add the layers to the map
var map = L.map('map', {
center: [33.9, 35.877],
zoom: 10,
layers: [ streets, cities]
});


//get length of the entries array
var len = "{{events|length}}";
var date = 1;
var time = 2;
var lat = 4;
var lon = 5;
//get events from database
var stri = "{% for event in events %} {{event.timestamp|date:"Y-m-d H"}} {{event.lat}} {{event.lon}} <br> {% endfor %}";
var entry = stri.split(" ");
//create the clustermarker object
var markers = new L.markerClusterGroup();
//create markers and add to cluster
var mymark;
for (var t = 0; t < len; t++) {
mymark = new L.marker([entry[lat], entry[lon]], {time: "\"" + entry[date] + entry[time] + "+01\""});
mymark.bindPopup("<b>Accident</b><br>this is marker number " + (t + 1) + " with coordinates :[" + entry[lat] + "," + entry[lon] + "]").openPopup();
markers.addLayer(mymark);
date += 8;
time += 8;
lat += 8;
lon += 8;
}
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
mymark = new L.marker([33.8,35.5]);
markers.addLayer(mymark);
//add cluster to map
// map.addLayer(markers);
//baseLayers for the map
var baseLayers = {
"Grayscale": grayscale,
"streets": streets
};
layerGroup = L.layerGroup(markers);
// $.getJSON("data.geojson", function(data) {

// var testlayer = L.geoJson(data);
var sliderControl = L.control.sliderControl({ position: "topright", layer: markers, range: false , follow: 3});
// ({position: "topright", layer: testlayer, follow: 3});
map.addControl(sliderControl);
sliderControl.startSlider();//});
</script>

抱歉有些评论,有些只是作为调试评论的实际代码

最佳答案

2016 年 3 月编辑:

您现在可以简单地使用插件 Leaflet.MarkerCluster.LayerSupport实现这一点,而无需更改 Leaflet Slider 插件代码。

查看演示 LayerSupport with LeafletSlider plugin .

在你的情况下,你会这样做:

var markers = L.layerGroup();

// Add all your markers into `markers` Layer Group.

// Check into MCG Layer Support!
// Add to map first before checking in.
L.markerClusterGroup.layerSupport().addTo(map).checkIn(markers);

var sliderControl = L.control.sliderControl({
position: "topright",
layer: markers,
range: false,
follow: 3
});

map.addControl(sliderControl);
sliderControl.startSlider();

Disclosure: I am the author of that plugin.


原答案:

避免将单个标记直接添加(删除)到(从) map 中,当它们由同时位于 map 上的 MarkerClusterGroup 处理时。

MCG 预计是唯一管理标记的组,如果你想显示/隐藏一些标记,你必须使用 markers.addLayer(myMarker);(或 removeLayer),其中 markers 是您的 MCG。

Leaflet Time-Slider (sliderControl) 插件因此与 MCG 不兼容:它直接向(从) map 添加(删除)标记。它对您的 MCG 一无所知。

不过,您应该能够通过将任何 map.addLayer 替换为 markers.addLayer 使其与 MCG 兼容(与 removeLayer 相同) ) 在插件代码中。不要忘记将您的 MCG 添加到 map 中。

关于javascript - 如何在 Javascript 中使用传单 slider 和 markercluster?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32886588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com