简体   繁体   English

单张动画标记创建重复的标记

[英]Leaflet animated marker create duplicated markers

I am using Leaflet with the animated marker plugin. 我正在将Leaflet与动画标记插件一起使用。 The main idea is to create an animated marker that goes to one place to another and, at the end, create another ( ONE ) animated marker in the same latitude and longitude that goes to another place and remove the first one. 主要思想是创建一个动画标记,该标记到一个地方到另一个地方,最后,在相同的经度和纬度上创建另一个动画标记( 一个 ),再到另一个地方,并删除第一个。

Right now, when the first animation ends it creates two animated markers. 现在,当第一个动画结束时,它将创建两个动画标记。

Here is a that part of the code: 这是代码的一部分:

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();
            }
        }
    });

}

EDIT: 编辑:

After a little debugging I found that onEnd function is running twice in the first animated marker and probably in the second marker as well, but I still don't know why this is happening. 经过一点调试后,我发现onEnd函数在第一个动画标记中可能运行了两次,可能在第二个标记中也运行了两次,但是我仍然不知道为什么会这样。

I just removed 我刚移走

marker.start();

And added this line when I create a marker 并在创建标记时添加了这一行

autoStart: true

So, the creation of the marker finally looks like this: 因此,标记的创建最终如下所示:

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);

It solved the problem. 它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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