perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

Leaflet routing machine gives me wrong route navigation #623

Closed fjerbi closed 3 years ago

fjerbi commented 3 years ago

Hello, I'm working on an ionic project, the problem here, everything is okay, it displays correctly the navigation in the control but in the map ,it shows me the wrong route navigation here is an example :

image

Here is some of my code :

this.map = new L.Map('mapinfoentreprise').setView([36.8065,10.1815], 15);
 const options = { profile: "mapbox/driving", polylinePrecision: 6 };

 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
 attribution:'Gaea21'
 }).addTo(this.map);
  var routeControl= L.Routing.control({
      waypoints: [
        L.latLng(36.8065, 10.1815),
        L.latLng(37.2768, 9.8642)
      ],
      router: (L.Routing as any).mapbox("api_key", options),
      routeWhileDragging: false,

    });

    var routeBlock = routeControl.onAdd(this.map);  
 // display the control in a div
    document.getElementById('controls').appendChild(routeBlock);
    routeControl.on('routesfound', function(e) {
      var routes = e.routes;
      var summary = routes[0].summary;    
      console.log('Total distance is ' + summary.totalDistance / 1000 + ' km and total time is ' + Math.round(summary.totalTime % 3600 / 60) + ' minutes');
    });

Did anyone faced this problem before ? and how can I fix it ?

fjerbi commented 3 years ago

Solved by adding this line

 let options = {
  enableHighAccuracy: true,
  maxAge: 0,
  timeout: 5000,
};

Thanks