gpt4 book ai didi

javascript - 使用传单获得界限

转载 作者:行者123 更新时间:2023-11-30 14:17:15 25 4
gpt4 key购买 nike

我有一个 DC.JSc 图表,它在我的传单 map 上过滤我的图标。基本上,当我过滤时,我希望我的 map 放大我选择的图标。

var onFilt = function(chart, filter) {
updateMap(locations.top(Infinity));
};

// Updates the displayed map markers to reflect the crossfilter dimension passed in
var updateMap = function(locs) {
// clear the existing markers from the map
markersLayer.clearLayers();
clusterLayer.clearLayers();

locs.forEach(function(d, i) {

if (d.latitude != null && d.latitude != undefined) {
// add a Leaflet marker for the lat lng and insert the application's stated purpose in popup

var mark = L.marker([d.latitude, d.longitude]);
markersLayer.addLayer(mark);
clusterLayer.addLayer(mark);

map.getBounds();
}
});
};

我试过:

map.getBounds(); //No response

L.markersLayer.getBounds(); //SCRIPT5007: Unable to get property 'getBounds' of undefined or null reference

map.fitBounds(markersLayer.getBounds()); // Object doesn't support property or method 'getBounds'

也试过:

if (d.latitude != null && d.latitude != undefined) {
d.ll = L.latLng(d.latitude, d.longitude);
var mark = L.marker([d.latitude, d.longitude]);
markersLayer.addLayer(mark);
clusterLayer.addLayer(mark);
};
map.addLayer(markersLayer);
map.fitBounds(markersLayer.getBounds());
});

Error: Object doesn't support property or method 'getBounds'

有什么想法吗?

找到了我自己的解决方案:map.fitBounds(clusterLayer.getBounds());

最佳答案

你快完成了,但是有几个错误:你试图在 forEach 循环中 getBounds,你试图 getBounds 来自错误的对象。

请查看并运行下面的代码片段,单击FILTER 按钮,阅读JS 代码中的注释。

我省略了过滤部分,只左缩放:

// add a map
var map = L.map('mapid').setView([51.505, -0.09], 12);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
}).addTo(map);

// Assuming that locations are filtered already:
var locations = [
{latitude: 51.5, longitude: -0.09},
{latitude: 51.53, longitude: -0.19},
{latitude: 51.45, longitude: 0},
{latitude: 51.56, longitude: 0.09}
];

// Updates the displayed map markers to reflect the crossfilter dimension passed in
var updateMap = function(locs) {
// clear the existing markers from the map
//markersLayer.clearLayers();
//clusterLayer.clearLayers();

var minlat = 200, minlon = 200, maxlat = -200, maxlon = -200;

locs.forEach(function(d, i) {

if (d.latitude != null && d.latitude != undefined) {
// add a Leaflet marker for the lat lng and insert the application's stated purpose in popup\
//var mark = L.marker([d.latitude, d.longitude]);
//markersLayer.addLayer(mark);
//clusterLayer.addLayer(mark);

// find corners
if (minlat > d.latitude) minlat = d.latitude;
if (minlon > d.longitude) minlon = d.longitude;
if (maxlat < d.latitude) maxlat = d.latitude;
if (maxlon < d.longitude) maxlon = d.longitude;

// set markers
L.marker([d.latitude, d.longitude]).addTo(map);
}
});

c1 = L.latLng(minlat, minlon);
c2 = L.latLng(maxlat, maxlon);

// fit bounds
map.fitBounds(L.latLngBounds(c1, c2));

// correct zoom to fit markers
setTimeout(function() {
map.setZoom(map.getZoom() - 1);
}, 500);

};

function filtr() {
updateMap(locations);
};
#mapid {
height: 180px;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />

<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>

<button onclick="filtr()">FILTER</button>

<div id="mapid"></div>

关于javascript - 使用传单获得界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53385989/

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