所以我想計算我的起點和多點之間的距離,而不是顯示到這一點的最短路線,但它總是顯示我最後一點。這是我的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);
所以請,如果有人有任何想法或解決方案,我將非常感激。
是的,你忘了在每個循環中選擇並寫入'end'。你從一個值獲得'Math.min'? – Bergi 2013-04-20 16:14:05
您是否基本上要求我們解決[旅行推銷員問題](http://en.wikipedia.org/wiki/Travelling_salesman_problem)? – deceze 2013-04-20 16:14:15
@deceze不,我不我剛纔一個錯誤在我的循環 – Mohammadov 2013-04-20 16:17:08