在dragend

时间:2015-12-03 01:17:06

标签: leaflet

我循环遍历数组并在地图上加载标记。如果标记被移动,我想更新数组元素的坐标。

到目前为止我的代码是这样的:

var m;
for (var i = 0; i < data.length; i++) {
  var plateNo = data[i].PLATE_NUMBER;
  var trackingNo = data[i].TRACKING_NUMBER;
  var inventoryId = data[i].INVENTORY_ID;
  if (data[i].INVENTORY_STATUS !== 'Complete') {
    var icon = epsMarker.incompleteIcon;
    var popup = '<h5>EPS</h5>' + 'Plate:' + plateNo + '<br/>' + '</p>';

    m = L.marker([data[i].REF_LATITUDE, data[i].REF_LONGITUDE], {
        icon: icon,
        draggable: 'true'
      })
      .bindPopup(popup);

    m.on('dragend', function (event) {
      var marker = event.target;
      var position = marker.getLatLng();
      console.log(position);
      //Call Database and Update Position by INVENTORY_ID 
    });
  }
}

我可以获得Lat和Long但我还想获得INVENTORY_ID参数。我们的想法是通过INVENTORY_ID查找数据库上的数据,然后更新lat和long。

我感谢任何帮助或指示。提前谢谢......

1 个答案:

答案 0 :(得分:3)

您可以将ID作为选项或成员属性存储在标记中,以便稍后检索它:

// Store as option
var marker = L.marker([0,0], {id: INVENTORY_ID}).addTo(map);
console.log(marker.options.id);

// Store as member property
var marker = L.marker([0,0]).addTo(map);
marker.id = INVENTORY_ID;
console.log(marker.id);