2013-04-20 185 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返回包括距离和持续时间信息。