Javascript 如何使用实时gps坐标更新标记的位置?
我有几辆车的实时gps位置,我想创建一个带有更新标记的地图。我的代码可以工作,但它不会更新标记,而是向传单地图添加具有新坐标的新对象。几分钟后,我的地图上满是标记。我做错了什么?这是我的基本概念 }) 函数getNewData{ $.getJSONserver{ 乐趣:GetGpsData, userId:user, sessionId:$sessionId.val },fillMap; } 函数fillMapjson{ 对于var i=0;iJavascript 如何使用实时gps坐标更新标记的位置?,javascript,leaflet,Javascript,Leaflet,我有几辆车的实时gps位置,我想创建一个带有更新标记的地图。我的代码可以工作,但它不会更新标记,而是向传单地图添加具有新坐标的新对象。几分钟后,我的地图上满是标记。我做错了什么?这是我的基本概念 }) 函数getNewData{ $.getJSONserver{ 乐趣:GetGpsData, userId:user, sessionId:$sessionId.val },fillMap; } 函数fillMapjson{ 对于var i=0;i
您当前的代码在这两种情况下都会生成新标记。如果您的目标是使用新的视觉外观和位置更新标记(如果它们已经存在于地图上),那么您应该在现有标记上使用和,而不是生成新标记
您当前的代码在这两种情况下都会创建新的标记。这里您的代码会根据需要进行更新。正如@snkashis所指出的,您不需要每次都创建一个新的标记
var devicePosition = {};
function fillMap(json) {
for (var i = 0; i < json.devicesData.length; i++) {
var data = json.devicesData[i];
var uniqueId = data.uniqueId;
if (data.positions) {
var index = data.positions.length - 1;
if (data.positions[index].lat && data.positions[index].lon) {
var latLng = L.latLng(parseFloat(data.positions[index].lat), parseFloat(data.positions[index].lon));
if (!devicePosition[uniqueId]) {
var devicePosition[uniqueId] = L.marker(latLng, {draggable : false})
.addTo(map);
} else {
devicePosition[uniqueId].setLatLng(latLng)
.update();
}
// Optional if you want to center the map on th marker
// map.panTo(latLng);
}
}
if (data.connected && devicePosition[uniqueId]) {
devicePosition[uniqueId].setIcon(data.connected ? 'onlineCarIcon' : 'offlineCarIcon');
}
}
在这里,您的代码已更新,可以按照您的需要工作。正如@snkashis所指出的,您不需要每次都创建一个新的标记
var devicePosition = {};
function fillMap(json) {
for (var i = 0; i < json.devicesData.length; i++) {
var data = json.devicesData[i];
var uniqueId = data.uniqueId;
if (data.positions) {
var index = data.positions.length - 1;
if (data.positions[index].lat && data.positions[index].lon) {
var latLng = L.latLng(parseFloat(data.positions[index].lat), parseFloat(data.positions[index].lon));
if (!devicePosition[uniqueId]) {
var devicePosition[uniqueId] = L.marker(latLng, {draggable : false})
.addTo(map);
} else {
devicePosition[uniqueId].setLatLng(latLng)
.update();
}
// Optional if you want to center the map on th marker
// map.panTo(latLng);
}
}
if (data.connected && devicePosition[uniqueId]) {
devicePosition[uniqueId].setIcon(data.connected ? 'onlineCarIcon' : 'offlineCarIcon');
}
}
var devicePosition = {};
function fillMap(json) {
for (var i = 0; i < json.devicesData.length; i++) {
var data = json.devicesData[i];
var uniqueId = data.uniqueId;
if (data.positions) {
var index = data.positions.length - 1;
if (data.positions[index].lat && data.positions[index].lon) {
var latLng = L.latLng(parseFloat(data.positions[index].lat), parseFloat(data.positions[index].lon));
if (!devicePosition[uniqueId]) {
var devicePosition[uniqueId] = L.marker(latLng, {draggable : false})
.addTo(map);
} else {
devicePosition[uniqueId].setLatLng(latLng)
.update();
}
// Optional if you want to center the map on th marker
// map.panTo(latLng);
}
}
if (data.connected && devicePosition[uniqueId]) {
devicePosition[uniqueId].setIcon(data.connected ? 'onlineCarIcon' : 'offlineCarIcon');
}
}