2014-03-25 2 views
0

사용자의 현재 위치에서 선택한 타겟까지의 경로를 만들려고합니다. 문제는 내가 사용자가 다음과 같습니다 내 경로 기능에/경도 위도 얻는 방법을 모르는입니다 :MapQuest Directions API를 사용하여 사용자로부터 경로 생성하기

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

기능은 위의 사용자가 예를 들어 A에 대한 관심의 포인트를 선택 할 때 호출되는 레스토랑을 선택하고 '경로 찾기'버튼을 클릭합니다. Leaflet locate 함수에 액세스 할 수 있지만이를 결합하여 위의 getRoute 함수로 사용자 geolocation을 얻는 방법을 알지 못합니다. 그것을 할 방법에 대한 제안? 건배!

답변

0

이 나를 위해 그것을했다 :

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

수정 getRoute 기능은 다음과 같습니다

가 호출 "대상 위치로 내 위치에서 경로 버튼을 찾아"이 기능을 클릭하면 :

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

이것은 유용 할 지 모르겠다.
geolocation을 사용하면 getRoute 함수로 전달되는 사용자 위도 및 경도를 찾을 수 있습니다.

<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> 
관련 문제