2017-04-20 60 views
0

我目前正在路由到标记,但我希望能够选择另一条路线并且当前标记将被重新路由。单击删除当前标记并在点击时添加一个新标记

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

回答

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; 

       } 
      } 
相关问题