admin管理员组文章数量:1302275
I'm trying to get the lat and long of a dragged and dropped marker on the Leaflet map.
However, I'm constantly getting TypeError: event.latlng is undefined
error.
This is my entire code:
var map = L.map('map');
googleStreets = L.tileLayer('http://{s}.google/vt/lyrs=m&x={x}&y={y}&z={z}',{
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3']
}).addTo(map);
map.locate({
setView: true,
maxZoom: 16,
watch:false,
timeout: 60000,
enableHighAccuracy: true
});
var marker;
var circles;
function onLocationFound(e) {
var radius = e.accuracy / 2;
if(map.hasLayer(circles) && map.hasLayer(marker)) {
map.removeLayer(circles);
map.removeLayer(marker);
}
marker = new L.Marker(e.latlng, {draggable:true});
circles = new L.circle(e.latlng, radius);
circles.bindPopup("You are within " + radius + " meters from this point").openPopup();;
map.addLayer(marker);
map.addLayer(circles);
marker.on('dragend', function(event) {
var mylat = event.latlng.lat
var result = marker.getLatLng();
console.log(mylat);
});
}
when I see inside the console, I see that error.
But when i change this console.log(mylat);
to this console.log(result);
, I see the lat and long in the console but the format of them is not what I am looking for.
This is what I get in the console when i drag and drop the marker the code with this console.log(result);
:
Object { lat=51.564025924107554, lng=0.7077598571777344, equals=function(), more...}
Could someone please advise on this?
Thanks in advance.
I'm trying to get the lat and long of a dragged and dropped marker on the Leaflet map.
However, I'm constantly getting TypeError: event.latlng is undefined
error.
This is my entire code:
var map = L.map('map');
googleStreets = L.tileLayer('http://{s}.google./vt/lyrs=m&x={x}&y={y}&z={z}',{
maxZoom: 20,
subdomains:['mt0','mt1','mt2','mt3']
}).addTo(map);
map.locate({
setView: true,
maxZoom: 16,
watch:false,
timeout: 60000,
enableHighAccuracy: true
});
var marker;
var circles;
function onLocationFound(e) {
var radius = e.accuracy / 2;
if(map.hasLayer(circles) && map.hasLayer(marker)) {
map.removeLayer(circles);
map.removeLayer(marker);
}
marker = new L.Marker(e.latlng, {draggable:true});
circles = new L.circle(e.latlng, radius);
circles.bindPopup("You are within " + radius + " meters from this point").openPopup();;
map.addLayer(marker);
map.addLayer(circles);
marker.on('dragend', function(event) {
var mylat = event.latlng.lat
var result = marker.getLatLng();
console.log(mylat);
});
}
when I see inside the console, I see that error.
But when i change this console.log(mylat);
to this console.log(result);
, I see the lat and long in the console but the format of them is not what I am looking for.
This is what I get in the console when i drag and drop the marker the code with this console.log(result);
:
Object { lat=51.564025924107554, lng=0.7077598571777344, equals=function(), more...}
Could someone please advise on this?
Thanks in advance.
Share Improve this question edited Nov 11, 2016 at 12:14 IvanSanchez 19.1k3 gold badges37 silver badges49 bronze badges asked Nov 11, 2016 at 10:43 JacksonJackson 82017 silver badges38 bronze badges 1- Does this answer your question? How do I get the latlng after the dragend event in leaflet? – Stephen Murumba Commented Apr 6, 2024 at 11:34
1 Answer
Reset to default 9You're listening to a L.Marker
's dragend
event. If you look at the documentation for that, you'll see that dragend
events fire event handlers which receive a DragEndEvent
, which only has distance
, type
and target
properties. No latlng
. That's expected.
Now, the target
property of a DragEndEvent
is the L.Marker
instance. So:
- You do not want the
LatLng
of theDragEndEvent
, because it doesn't even exist - You want the
LatLng
of theL.Marker
which is thetarget
of theDragEndEvent
.
And how do you get the LatLng
of a L.Marker
? With its getLatLng()
method. So:
marker.on('dragend', function(event) {
var latlng = event.target.getLatLng();
console.log(latlng.lat, latlng.lng)
});
See a working example.
本文标签: javascriptHow to get the LatLng of a quotdragendquot event in LeafletStack Overflow
版权声明:本文标题:javascript - How to get the LatLng of a "dragend" event in Leaflet? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741710050a2393789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论