简体   繁体   中英

Leaflet animated marker not displaying the latitude and longitude

I am using this code it shows the message in a popup when I remove the e.latlng.lat and + e.latlng.lng

var myMovingMarker = L.Marker.movingMarker([
  [23.59582641820334, 58.439605236053474],
  [21.5278654, 55.9196996]
], [100000], {
  icon: orangeIcon
}, {
  title: "MyPoint",
  alt: "The Big I",
  draggable: true
}, )

var popup = L.popup({
  keepInView: false,
  autoPan: false,
  closeButton: false,
  closeOnClick: true,
  maxWidth: 1000

}).setContent("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)

myMovingMarker.bindPopup(popup).openPopup()

The variable e is not defined. Additionally setContent() set a static content, this means when you add text with setContent() is will not updated, even if the marker latlng has changed.

You have to set the content every time the popup is opened:

myMovingMarker.on('popupopen',function(e){
    var markerLatLng = e.popup._source.getLatLng();
    e.popup.setContent("Lat, Lon : " + markerLatLng.lat + ", " + markerLatLng.lng)
  })

Another way would be to updated the content every time the marker is moved:

myMovingMarker.on('move',function(e){
    var markerLatLng = e.target.getLatLng();
    popup.setContent("Lat, Lon : " + markerLatLng.lat + ", " + markerLatLng.lng)
  })

PS: Both not tested, but should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM