The shortest distance between multiple points

核能气质少年 提交于 2019-12-13 06:26:25

问题


So i want to calculate distance between my start point and multiple points, than display the shortest route to this point,but it show me always the last point. this is my distanceCal function it works fine :

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

and this is my points latt/long :

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

then my script to display directions :

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

so please if someone have any idea or solution i will be very appreciate.


回答1:


The following code uses Googles geometry library to calculate distances between points.The distances are stored in an array and then parsed to find minimum distance .

I have changed the array from dist[] to coords[] as we need an array to hold distances 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



回答2:


It sounds like you want to use optimizeWaypoints:true in your DirectionsServiceRequest:

optimizeWaypoints | boolean | If set to true, the DirectionService will attempt to re-order the supplied intermediate waypoints to minimize overall cost of the route. If waypoints are optimized, inspect DirectionsRoute.waypoint_order in the response to determine the new ordering.

The DirectionsResult

Each leg of each route returned includes distance and duration information.



来源:https://stackoverflow.com/questions/16122381/the-shortest-distance-between-multiple-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!