最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Leaflet marker is not draggable even if draggable=true - Stack Overflow

programmeradmin2浏览0评论

I'm using a Leaflet map with markers.

When the user clicks "edit" on my page, I want to make the markers draggable. If I set the property draggable to true for each marker, it doesn't work.

When I create a new marker and set the property right from the beginning, it works.

I'm using a Leaflet map with markers.

When the user clicks "edit" on my page, I want to make the markers draggable. If I set the property draggable to true for each marker, it doesn't work.

When I create a new marker and set the property right from the beginning, it works.

Share Improve this question edited Jan 21, 2017 at 22:47 approxiblue 7,12216 gold badges52 silver badges59 bronze badges asked Apr 8, 2013 at 20:36 ada91ada91 8282 gold badges7 silver badges19 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

You gotta do it like this:

marker.dragging.disable(); // marker.dragging.enable();

My first attempt only changes a technical property but not the behavior.

Motivated by @mc0e problem concerning 'undefined'. Here is an example.

(Based on the code from Leafletjs.com and Leafletjs quick start)

  1. Open Wikimedia maps (which is based on the framework in question).
  2. Open browser console (Ctrl+j or Ctrl+k) to put a Marker (you will need to define a variable first). Use Code-1.

Code-1 in console:

var markerLondon = L.marker()
    .setLatLng([51.5, -0.09])
    .bindPopup('Centre of London')
    .addTo(map)
    .openPopup();
  1. Now you have a non-draggable marker. Use Code-2 to make the Marker draggable.

Code-2 in console:

markerLondon.dragging.enable();

Further reading: Marker API reference.

PS: Once Wikimedia starts using something else, you may test it with BigMap 2 (also based on the same framework) created for making static OpenStreetMap images.

发布评论

评论列表(0)

  1. 暂无评论