Leaflet删除当前标记并在单击时添加新标记

时间:2017-04-20 10:10:28

标签: django routing leaflet coordinates gis

我目前正在路由到标记,但我希望能够选择其他路由,然后重新路由当前标记。

图1显示了当前的路由。

点击“路线到这里”'按钮必须删除当前标记并替换为新标记。

Image current routing

OnClick this should remove the current marker and add a new marker

function showPosition(position) 
    {

    //Set the map view to be the users location
    //

    var map = L.map('map').setView([position.coords.latitude, position.coords.longitude], 14);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    //Change the users marker to a unique red & show users location on click
    //

    L.marker([position.coords.latitude, position.coords.longitude], {
        icon: L.AwesomeMarkers.icon({prefix: 'fa', markerColor: 'red'})
    }).addTo(map).bindPopup("<b>Your location: </b>" + position.coords.latitude + "," + position.coords.longitude);

    //Routing users location to the desired route
    //

    L.Routing.control({
    waypoints: [L.latLng(users_lat_coords, users_lng_coords), L.latLng(x, y)],
                lineOptions: {addWaypoints: false}
        }
    ).addTo(map);
}

1 个答案:

答案 0 :(得分:0)

传单路由计算机库的开发人员仍在使用代码来实现此目的,因此已完成解决方法。如果先前已经建立了路线,我会拼接航路点并在其中添加一个新航线。

var routing ='';
var been_routed=false;
function route_to_station(point_lat_coords, point_lng_coords, x1, y1) {
                users_lat_coords = point_lat_coords;
                users_lng_coords = point_lng_coords;
                x = x1;
                y = y1;

                if (x !== '') {
                    if (been_routed === true) {
                        routing.spliceWaypoints(0, 1);
                    }
                    routing = L.Routing.control({
                            waypoints: [L.latLng(users_lat_coords, users_lng_coords), L.latLng(x, y)],
                            lineOptions: {addWaypoints: false}

                        }
                    );
                    routing.addTo(map);
                    been_routed = true;

                }
            }