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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

You 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