admin管理员组文章数量:1328015
Here's my code
var map = L.mapbox.map('map', 'examples.map-20v6611k').setView([37.9, -77],4);
var marker = L.marker(new L.LatLng(37.9, -77), {
icon: L.mapbox.marker.icon({'marker-color': 'CC0033'}),
draggable: true
});
marker.bindPopup('This marker is draggable! Move it around.');
marker.addTo(map);
//my code
marker.on('mousedown', function(e){
location = marker.getLatLng();
document.getElementById('locationBox').innerHTML = "poop";
});
The marker on the map can be dragged anywhere. I'm trying to take the longitude and latitude of the marker after the marker has been dragged somewhere which is why I'm using the marker.on('mousedown', function(e){ event handler. I have two problems. I'm trying to nest another event handler
marker.on('mouseup', function(e){
inside that so the location is grabbed once the marker is let go from being dragged but the 'mouseup' method doesn't work.
Second problem
location = marker.getLatLng();
Doesn't store lat/lon in location but rather just opens a new URL I haven't configured for.
How can I use Javascript or Jquery to grab the location of the marker after it is dragged and then dropped, and how can I appropiately store the lat/lon in a variable?
Thanks!
Here's my code
var map = L.mapbox.map('map', 'examples.map-20v6611k').setView([37.9, -77],4);
var marker = L.marker(new L.LatLng(37.9, -77), {
icon: L.mapbox.marker.icon({'marker-color': 'CC0033'}),
draggable: true
});
marker.bindPopup('This marker is draggable! Move it around.');
marker.addTo(map);
//my code
marker.on('mousedown', function(e){
location = marker.getLatLng();
document.getElementById('locationBox').innerHTML = "poop";
});
The marker on the map can be dragged anywhere. I'm trying to take the longitude and latitude of the marker after the marker has been dragged somewhere which is why I'm using the marker.on('mousedown', function(e){ event handler. I have two problems. I'm trying to nest another event handler
marker.on('mouseup', function(e){
inside that so the location is grabbed once the marker is let go from being dragged but the 'mouseup' method doesn't work.
Second problem
location = marker.getLatLng();
Doesn't store lat/lon in location but rather just opens a new URL I haven't configured for.
How can I use Javascript or Jquery to grab the location of the marker after it is dragged and then dropped, and how can I appropiately store the lat/lon in a variable?
Thanks!
Share Improve this question asked Jun 24, 2013 at 3:13 Conor PatrickConor Patrick 3,0895 gold badges24 silver badges33 bronze badges2 Answers
Reset to default 6You might want to use the dragend
event as it fires after a marker has been dragged. If you peer into what e.target
returns there is a _latlng
object that gives you the details you need:
marker.on('dragend', function(e){
console.log(e.target._latlng);
});
An underscore used in a method name implies that its private so there may be a more appropriate way to access this but for now should suit your needs.
You can also do this:
console.log(marker._lngLat.lat);
console.log(marker._lngLat.lng);
本文标签: javascriptHow can I find the latlon of a marker in MapBoxStack Overflow
版权声明:本文标题:javascript - How can I find the latlon of a marker in MapBox? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243519a2438982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论