Leaflet js:如何在Map上绘制任何标记的末尾获取Lat和lng [英] Leaflet js: How to get Lat and lng at the end of drawing any marker on Map

查看:344
本文介绍了Leaflet js:如何在Map上绘制任何标记的末尾获取Lat和lng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个接近的代码,但我不确定,因为我第一次使用Leaflet js。

i got a one code which is close but i am not sure because i am first time working with Leaflet js.

我的意图是:假设Leaflet js正在显示地图或非地理数据,我有一个与Leaflet js相关的工具栏。当用户点击工具栏上的任何标记开始在地图上绘图时,工具栏上有许多标记,如圆形,多边形,方形等。当标记的绘制完成时,我怎么知道?

my intention is: suppose Leaflet js is showing map or non geographical data and i have one Leaflet js related toolbar. toolbar has many marker like circle, poly, square etc. when user click on any marker on toolbar to start drawing on map then when drawing of marker will be finish then how do i know?

我怎么能附加一个函数,它将作为绘图完成的回调函数,该函数也让我知道标记的Lat和lng。如果我知道Lat和lng然后我可以保存在db中,稍后我们可以重复使用它来绘制形状。

how could i attach a function which will work as callback for drawing finish and the function also let me know the Lat and lng of marker. if i know the Lat and lng then i can save in db and later we can reuse it to draw the shape.

var map = L.map('mapcanvas').setView([51.505, -0.09], 13);

map.on('click', function(e){
    // Place marker
    var marker = new L.marker(e.latlng).addTo(map);

    // Ajax query to save the values:
    var data = {
        lat: e.latlng.lat,
        lng: e.latlng.lng
    }

    // saving lat and lng to db by ajax call
    var request = new XMLHttpRequest();
        request.open('POST', '/my/url', true);
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        request.send(data);
});

请看一下代码,我不确定它是否解决了我的目的,因为这里代码使用带地图的点击事件。

please have a look at the code and i am not sure does it solve my purpose because here the code work with click event with map.

因此,当我们点击地图时,标记绘图将完成。所以请阅读我的帖子,了解我在寻找什么,并建议我如何实现它。谢谢

so when we click on map then marker drawing would be complete. so please read my post and understand what i am looking for and suggest me with best idea how to achieve it. thanks

推荐答案

取决于你用来绘制多边形/形状的内容。如果你正在使用leaflet.draw,你会做类似的事情:

Depends on what you're using to draw polygons/shapes. If you're using leaflet.draw, you would do something like:

map.on('draw:created', function(e) {
    var points = JSON.stringify(e.layer.toGeoJSON());
});

然后将积分保存到您的数据库。

And then saving points to your database.

阅读leaflet.draw 文档,它非常完整和直接。

Read the leaflet.draw documentation, it's pretty complete and straightforward.

这篇关于Leaflet js:如何在Map上绘制任何标记的末尾获取Lat和lng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆