2013-07-23 4 views
0

Google지도를 사용하면 두 주소 사이에 가능한 다른 경로를 얻을 수 있으며 모든 가능한 경로의 길이를 얻을 수 있습니다.두 주소 간의 최단 경로

enter image description here 저는 이제 google map으로 웹 사이트에서 작업하고 있습니다. Google지도와 동일한 두 가지 주소 사이의 최단 경로를 계산하고 싶습니다. 그런 다음 javascript 변수로 경로 길이를 다시 가져옵니다.

google.maps.geometry.spherical.computeDistanceBetween (latLng1, latLng1); 

인수는 두 LatLng를 객체 :

+0

. 각 경로에는 거리와 기간이 포함됩니다. [개발자 안내서] (https://developers.google.com/maps/documentation/javascript/directions) - [DirectionsService 참조] (https://developers.google.com/maps/documentation/javascript/reference#DirectionsRequest), provideRouteAlternatives를 참조하십시오 - 라우트 대체가 제공되어야하는지 여부. 선택 과목. – geocodezip

답변

0

내가 그것을 발견, 그것은 The recommended path's lengthDuration를 포함하는 JSON 응답을 반환 Distance Matrix라는 구글지도 API를 사용하여 계산 될 수있다.

{ 
"destination_addresses" : [ "Témara, Maroc" ], 
"origin_addresses" : [ "Rabat, Maroc" ], 
"rows" : [ 
    { 
    "elements" : [ 
     { 
      "distance" : { 
       "text" : "13,6 km", 
       "value" : 13620 
      }, 
      "duration" : { 
       "text" : "23 minutes", 
       "value" : 1358 
      }, 
      "status" : "OK" 
     } 
    ] 
    } 
], 
"status" : "OK" 
} 

이 잘 작동합니다 : 당신은 구글지도 API V3를하려면 DirectionsService와 대체 경로를 얻을 수 있습니다

function getDistance() 
{ 
var url="http://maps.googleapis.com/maps/api/distancematrix/json?origins=rabat&destinations=temara&mode=driving&language=fr-FR&sensor=false"; 
var def = new jQuery.Deferred(); 
$.getJSON(url,function(json) 
{ 
if (json.status=="OK") 
{ 
var distance=json.rows[0].elements[0].distance.text; 
} 
def.resolve(distance); 
}); 
return def.promise(); 
} 

$.when(getDistance()).then(function(distance){ 
alert(distance);}); 

Demo

1

는 이러한 기능을 사용할 수 있습니다.
은 포함해야합니다 :

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script> 
+0

무엇이 반환됩니까? –

+0

최단 경로 –

+0

하지만이 두 LatLng 객체 사이의 측지선 길이를 계산합니다. 경로가 존재하는지 여부가 결정됩니다. –