我正试图摆脱Google地图,因为我已经厌倦了它们现在这么慢的速度!我决定试一试Leaflet,这似乎是个不错的选择。下面是我的代码:
initMap();
function initMap() {
window.VARS.Map_Modal = L.map('google_map_modal_inner', {
center: [window.VARS.lat,window.VARS.lng],
zoom: 10,
maxZoom: 20,
zoomControl: false
});
new L.Control.Zoom({ position: 'topright' }).addTo(window.VARS.Map_Modal);
setTimeout(function() {
window.VARS.Map_Modal.invalidateSize();// stop the grey tiles due to being a flex div
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'xxx'
}).addTo(window.VARS.Map_Modal);
}, 500);
getMarkers();
}
function getMarkers() {
var extra_params = get_extra_params();
extra_params["action"] = "load_results_cat";
// other params pass in
var the_url = '/cgi-bin/links/ajax.cgi';
reqwest({
url: the_url,
type: 'json',
method: 'post',
data: extra_params,
// timeout: 10000,
error: function (err) {
alert("ERROR");
},
success: function (data) {
window.VARS.markers = []; // empty
window.VARS.marker_vals = []; // empty
window.VARS.markerCluster = L.markerClusterGroup();
data.markers.forEach(function(item, i){
window.VARS.markerCluster.addLayer(
L.marker([item.latitude, item.longitude])
.on("click", function(marker) {
console.dir(marker);
})
);
});
window.VARS.Map_Modal.addLayer(window.VARS.markers);
}
});
}
function update_bottom_bar_visible() {
var mapBounds = window.VARS.Map_Modal.getBounds();
for (var i = window.VARS.markers.length -1; i >= 0; i--) {
var m = window.VARS.markers[i];
var shouldBeVisible = mapBounds.contains(m.getLatLng());
if (shouldBeVisible) {
console.log("This is visible");
} else {
console.log("This NOT visible");
}
}
}
一切正常,地图显示正常。我遇到的问题与以下代码有关:
var shouldBeVisible = mapBounds.contains(m.getLatLng());
if (shouldBeVisible) {
console.log("This is visible");
} else {
console.log("This NOT visible");
}
我无法让它遍历标记。我需要做的是浏览所有可见的标记,然后在下面的栏中显示它们。我已经删除了这个脚本的冗长篇幅-希望这样就足够了。
这是群集工具的一个有效示例,但它没有在视口中选择当前标记。这就是我需要帮助的地方:)
https://jsfiddle.net/youradds/g7fd0k5z/1/
如果您需要更多信息,请告诉我
更新2:我设法从其他人那里找到了一些代码,
var i = 0;
window.VARS.markerCluster.eachLayer(function (layer) {
var lat = layer.getLatLng().lat;
var lng = layer.getLatLng().lng;
console.dir("marker number("+i+"):"+lat+","+lng);
//array.push({name:location,lat:lat,lng:lng});
i++;
});
这可以获取每个图层的值,但是不包括集群中的当前标记。您如何提取呢?
答案 0 :(得分:0)
好,所以我知道了:
window.VARS.markerCluster.eachLayer(function (layer) {
var lat = layer.getLatLng().lat;
var lng = layer.getLatLng().lng;
var shouldBeVisible = mapBounds.contains([lat,lng]);
if (shouldBeVisible) {
console.log("This is visible");
} else {
console.log("This NOT visible");
}
});