Question

I'm trying to create a route from the users current position to the target he chose. The problem is, i don't know how to get the users Latitude/Longitude into my route function which looks like this:

getRoute = function(){  
    dir = MQ.routing.directions()
        .on('success', function(data) {
            //does some stuff with the routes data/directions. not important here
        }); 

    dir.route({
        locations: [
            { latLng: { lat: USER LAT HERE, lng: USER LNG HERE } },         
            { latLng: { lat: (poiCoordinates.lat), lng: (poiCoordinates.lng) } }
        ],
        options: {          
            //not important as well         
        }
    }); 

    mqroute = MQ.routing.routeLayer({
        directions: dir,       
    }).addTo(map);
};

The function above is called when the user selects a point of interest, for example a restaurant, and clicks the "find route" button. I have access to a Leaflet locate function, but don't know how to combine them and get the users geolocation into the getRoute function above. Any suggestions for a way to do it? Cheers!

Was it helpful?

Solution 2

This did it for me:

Clicking the "find the route button from my position to the targets position" calls this function:

createRoute = function(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(getRoute);
    } else {
        alert("Geolocation not supported");
    }
};

The modified getRoute function looks like this:

getRoute = function(position){

    userLatitude = position.coords.latitude;
    userLongitude = position.coords.longitude;

    dir = MQ.routing.directions()
        .on('success', function(data) {
            //does some stuff with the routes data/directions. not important here
        }); 

    dir.route({
        locations: [
            { latLng: { lat: userLatitude, lng: userLongitude } },         
            { latLng: { lat: (poiCoordinates.lat), lng: (poiCoordinates.lng) } }
        ],
        options: {          
            //not important as well         
        }
    }); 

    mqroute = MQ.routing.routeLayer({
        directions: dir,       
    }).addTo(map);
};

OTHER TIPS

This is just an example I do not know if you will be useful.
Using geolocation you can find user latitude and longitude that are passed to getRoute function.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    getRoute = function(latitude,longitude){ 
        //.........
        console.log(latitude,longitude)
        dir.route({
            locations: [
                { latLng: { lat: latitude, lng: longitude } }, 
                { latLng: { lat: (poiCoordinates.lat), lng: (poiCoordinates.lng) } }
            ],
        }); 

      //....... 
    };
   if (navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(success,error);
       }else{alert("Geolacion not supported")};

   function success(pos) {
       var latx=pos.coords.latitude;
       var longx=pos.coords.longitude;
       getRoute(latx,longx)
  };
  function error(pos) {
     alert('error')
  };         
});
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top