javascript - 如何将 e.latlng 存储到数组 : leaflet

标签 javascript leaflet angular-leaflet-directive

我正在使用传单 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/

相关文章:

javascript - Django 网站未正确检查链接

R Shiny Leaflet- 从测量工具获取数据

leaflet - 如何使用角度传单指令和 geojson 更改传单弹出窗口的偏移量?

javascript - 有没有办法返回当前不在聚类组内的所有标记的列表?

javascript - AngularJS - Leaflet - "center"属性未在主范围内定义

javascript - 如何将数据从 Controller 放入 View 中的特定 div 元素中

javascript - 迭代表列并构建 JSON - jQuery

javascript - 传单不渲染 map

javascript - 检查一个 div 是否有子元素 - Jquery

Vue.js 3 在运行时挂载组件实例