我正在使用传单 API,用户可以在 map 上放置标记。我制作了一个用于放置标记的自定义按钮。
我愿意在这些标记之间画线,即使用
L.polylines()
但由于我是 javascript 和 leaflet 的新手,所以我不能
了解如何将这些 latlng 点传递给稍后将要介绍的数组
在这些函数中使用。对于初始工作,我已经通过了静态
坐标(按要求工作)。
L.easyButton('fa-link', function () {
var secureThisArea = [[-81, 100.75], [-76.50, 245.75], [-145.50, 184.25], [-128, 311.75]];
map.on('click', function fencePlace(e) {
L.marker([-81, 100.75], { icon: fenceIcon, draggable: true }).bindPopup("this is first").addTo(map);
L.marker([-76.50, 245.75], { icon: fenceIcon, draggable: true }).bindPopup("this is second").addTo(map);
L.marker([-145.50, 184.25], { icon: fenceIcon, draggable: true }).bindPopup("this is third").addTo(map);
L.marker([-128, 311.75], { icon: fenceIcon, draggable: true }).bindPopup("this is fourth").addTo(map);
L.polyline(secureThisArea).addTo(map);
});
}).addTo(map);
最佳答案
向数组添加另一个值很容易,例如:
secureThisArea.push([-81, 100.75]);
您可以在 Mozilla Developer Network 找到更多详细信息(以及与 JavaScript 相关的其他任何内容) .
如果您想使用标记对象的坐标,您可以通过以下方式获取:
var myMarker = L.marker([-81, 100.75], { icon: fenceIcon, draggable: true }),
latLng = null;
latLng = myMarker.getLatLng();
另请查看Leaflet documentation .
关于javascript - 如何将 e.latlng 存储到数组 : leaflet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564673/