繁体   English   中英

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

[英]Leaflet animated marker create duplicated markers

我正在将Leaflet与动画标记插件一起使用。 主要思想是创建一个动画标记,该标记到一个地方到另一个地方,最后,在相同的经度和纬度上创建另一个动画标记( 一个 ),再到另一个地方,并删除第一个。

现在,当第一个动画结束时,它将创建两个动画标记。

这是代码的一部分:

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

它解决了问题。

暂无
暂无

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

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