2013-04-20 178 views
0

所以我想計算我的起點和多點之間的距離,而不是顯示到這一點的最短路線,但它總是顯示我最後一點。這是我的distanceCal功能正常工作:多點之間的最短距離

function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) { 
var R = 6371; // Radius of the earth in km 
var dLat = deg2rad(lat2 - lat1); // deg2rad below 
var dLon = deg2rad(lon2 - lon1); 
var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
; 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
var d = R * c; // Distance in km 
return d; 
} 

function deg2rad(deg) { 
return deg * (Math.PI/180) 
} 

,這是我的觀點LATT /長:

var dist = [ 
    [35.733972, -5.881999], 
    [ 35.734077, -5.881033], 
    [ 35.736898, -5.877771], 
    [35.738396, -5.875154] 
    ]; 

然後我的腳本顯示方向:

function calcRoute() { 
var start = new google.maps.LatLng(35.728329, -5.882750); 
for (var i = 0; i < dist.length; i++) 
{ 
    var dis = dist[i]; 
    //here i need something to choose the shortest route 
    var min = Math.min(getDistanceFromLatLonInKm(35.728329, -5.882750, dis[0], dis[1])); 
    var end = new google.maps.LatLng(dis[0], dis[1]); 
} 
    var request = { 
     origin: start, 
     destination: end, 
     optimizeWaypoints: true, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 

directionsService.route(request, function (response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
    } 
}); 

} 
google.maps.event.addDomListener(window, 'load', getMap); 

所以請,如果有人有任何想法或解決方案,我將非常感激。

+0

是的,你忘了在每個循環中選擇並寫入'end'。你從一個值獲得'Math.min'? – Bergi 2013-04-20 16:14:05

+0

您是否基本上要求我們解決[旅行推銷員問題](http://en.wikipedia.org/wiki/Travelling_salesman_problem)? – deceze 2013-04-20 16:14:15

+0

@deceze不,我不我剛纔一個錯誤在我的循環 – Mohammadov 2013-04-20 16:17:08

回答

1

下面的代碼使用Google的geometry庫來計算百分點。距離之間的距離存儲在數組中,然後分析以查找最小距離。

我從DIST []數組改爲COORDS [],因爲我們需要一個數組來保存距離DIST []。

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script> 
<script type="text/javascript"> 
var coords = [ 
    [35.733972, -5.881999], 
    [35.734077, -5.881033], 
    [35.736898, -5.877771], 
    [35.738396, -5.875154] 
    ]; 
    var dist = [];//Array to hold distances 
    function calcRoute() { { 
     var start = new google.maps.LatLng(35.728329, -5.882750); 
     for (var i = 0; i < coords.length; i++){ 
     var point = new google.maps.LatLng(coords[i][0],coords[i][1]); 
     var distance = google.maps.geometry.spherical.computeDistanceBetween(start, point); 
     dist.push(distance); 
     } 
     var test = dist[0]; 
    var index = 0; 
    for (var i = 1; i < dist.length; i++){ 
     if(dist[i] < test){ 
      test = dist[i]; 
      index = i; 
     } 
    } 
    var end = new google.maps.LatLng(coords[index][0],coords[index][1]); 

     // Apply the rest of your code here 
+0

感謝大衛的代碼是優秀的,但此行是錯誤的** VAR端=新的谷歌更快.maps.LatLng(sortArray [0] [0],sortArray [0] [1]); **當我改變它爲sortArray [0],sortArray [1]我得到距離數組 – Mohammadov 2013-04-20 22:39:44

+0

我會改變最後一行你是正確的我測試了除最後一行以外的所有內容 – 2013-04-20 23:50:37

+0

@Mohammadov我已修改答案提供解決方案 – 2013-04-21 00:30:40

0

這聽起來像你想使用optimizeWaypoints:真正在你的DirectionsServiceRequest

optimizeWaypoints |布爾|如果設置爲true,DirectionService將嘗試重新排列提供的中間航點以最小化路線的總體成本。如果優化了航點,請在響應中檢查DirectionsRoute.waypoint_order以確定新的排序。

DirectionsResult

每個leg of each route返回包括距離和持續時間信息。