2017-04-20 61 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; 

       } 
      } 
相關問題