传单动画标记创建重复的标记
Leaflet animated marker create duplicated markers
我正在使用带动画标记插件的 Leaflet。主要思想是创建一个从一个地方到另一个地方的动画标记,最后,在相同的纬度和经度上创建另一个 (ONE) 动画标记去另一个地方并删除第一个。
现在,当第一个动画结束时,它会创建两个 个动画标记。
这是那部分代码:
function moveDriverToPassengerLocation(driver, passenger) {
// Creating the request for google's direction services
var request = {
origin: driver.startLocation,
destination: passenger.startLocation,
travelMode: 'DRIVING'
};
// Sending the request
directionsService.route(request, function (result, status) {
// Verify if the status is ok for getting the path
if (status == 'OK') {
// Decoding the polyline
var decodedPath = L.PolylineUtil.decode(
result.routes[0].overview_polyline);
// Verify if the path has more than one point
if (decodedPath.length > 1) {
var line = L.polyline(decodedPath);
// Creating the new animated marker
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
moveDriverToPassengerDestination(driver, passenger)
}
}).addTo(map);
marker.id = driver.id;
marker.startLocation = driver.startLocation;
driversMarkers.push(marker);
marker.start();
}
}
});
}
function moveDriverToPassengerDestination(driver, passenger) {
// Creating the request for google's direction services
var request = {
origin: passenger.startLocation,
destination: passenger.destination,
travelMode: 'DRIVING'
};
// Sending the request
directionsService.route(request, function (result, status) {
// Verify if the status is ok for getting the path
if (status == 'OK') {
// Decoding the polyline
var decodedPath = L.PolylineUtil.decode(result.routes[0]
.overview_polyline);
// Verify if the path has more than one point
if (decodedPath.length > 1) {
var line = L.polyline(decodedPath);
// Creating the new animated marker
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
}
}).addTo(map);
marker.id = driver.id;
marker.startLocation = driver.startLocation;
driversMarkers.push(marker);
marker.start();
}
}
});
}
编辑:
经过一些调试,我发现 onEnd 函数在第一个动画标记中 运行 两次,可能在第二个标记中也是如此,但我仍然不知道为什么会这样。
我刚刚删除了
marker.start();
并在我创建标记时添加了这一行
autoStart: true
因此,标记的创建最终如下所示:
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
autoStart: true,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
driver.isMoving = false;
addDriverMarker(driver);
}
}).addTo(map);
问题解决了。
我正在使用带动画标记插件的 Leaflet。主要思想是创建一个从一个地方到另一个地方的动画标记,最后,在相同的纬度和经度上创建另一个 (ONE) 动画标记去另一个地方并删除第一个。
现在,当第一个动画结束时,它会创建两个 个动画标记。
这是那部分代码:
function moveDriverToPassengerLocation(driver, passenger) {
// Creating the request for google's direction services
var request = {
origin: driver.startLocation,
destination: passenger.startLocation,
travelMode: 'DRIVING'
};
// Sending the request
directionsService.route(request, function (result, status) {
// Verify if the status is ok for getting the path
if (status == 'OK') {
// Decoding the polyline
var decodedPath = L.PolylineUtil.decode(
result.routes[0].overview_polyline);
// Verify if the path has more than one point
if (decodedPath.length > 1) {
var line = L.polyline(decodedPath);
// Creating the new animated marker
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
moveDriverToPassengerDestination(driver, passenger)
}
}).addTo(map);
marker.id = driver.id;
marker.startLocation = driver.startLocation;
driversMarkers.push(marker);
marker.start();
}
}
});
}
function moveDriverToPassengerDestination(driver, passenger) {
// Creating the request for google's direction services
var request = {
origin: passenger.startLocation,
destination: passenger.destination,
travelMode: 'DRIVING'
};
// Sending the request
directionsService.route(request, function (result, status) {
// Verify if the status is ok for getting the path
if (status == 'OK') {
// Decoding the polyline
var decodedPath = L.PolylineUtil.decode(result.routes[0]
.overview_polyline);
// Verify if the path has more than one point
if (decodedPath.length > 1) {
var line = L.polyline(decodedPath);
// Creating the new animated marker
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
}
}).addTo(map);
marker.id = driver.id;
marker.startLocation = driver.startLocation;
driversMarkers.push(marker);
marker.start();
}
}
});
}
编辑:
经过一些调试,我发现 onEnd 函数在第一个动画标记中 运行 两次,可能在第二个标记中也是如此,但我仍然不知道为什么会这样。
我刚刚删除了
marker.start();
并在我创建标记时添加了这一行
autoStart: true
因此,标记的创建最终如下所示:
var marker = L.animatedMarker(line.getLatLngs(),
{
distance: 300,
interval: 2000,
autoStart: true,
icon: taxiIcon,
onEnd: function() {
map.removeLayer(this);
driver.isMoving = false;
addDriverMarker(driver);
}
}).addTo(map);
问题解决了。