以公里为单位的最短距离 google 地图

Shortest distance in km google map

我在 google 表中使用此脚本来计算我的里程费用。

    /**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/

function GOOGLEMAPS(start_address,end_address,return_type) {

  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed
  
  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  var directions = mapObj.getDirections();
  
  var getTheLeg = directions["routes"][0]["legs"][0];
  
  var meters = getTheLeg["distance"]["value"];
  
  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "Error: Wrong Unit Type";
   }
  
}

工作正常,但我希望脚本选择距离而不是时间上的最短路线,如本例所示:

它给了我 22.7 公里,但我希望它每次都给我最短距离,在本例中为 21.3 公里

我可以在脚本中更改什么以使其采用距离而不是时间上的最短路线?谢谢!

我相信你的目标如下。

  • 您想检索最小距离。
  • 您想使用 Google Apps 脚本的自定义函数来实现此目的。

在这种情况下,为了检索多条路线,我使用了setAlternatives(useAlternatives)。并且,从检索到的多条路线中检索出最小距离。

当这反映在您的脚本中时,它将变成如下。请按如下方式修改您的脚本。

发件人:

var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();

var getTheLeg = directions["routes"][0]["legs"][0];

var meters = getTheLeg["distance"]["value"];

收件人:

var mapObj = Maps.newDirectionFinder().setAlternatives(true);
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
var getTheLeg = directions["routes"][0]["legs"][0];
var meters = Math.min(...directions.routes.map(({legs: [{distance: {value}}]}) => value));
  • 如果要从meters的同一个routes中使用getTheLeg的值,请修改如下。

    • 来自

        var mapObj = Maps.newDirectionFinder();
        mapObj.setOrigin(start_address);
        mapObj.setDestination(end_address);
        var directions = mapObj.getDirections();
      
        var getTheLeg = directions["routes"][0]["legs"][0];
      
        var meters = getTheLeg["distance"]["value"];
      
    •   var mapObj = Maps.newDirectionFinder().setAlternatives(true);
        mapObj.setOrigin(start_address);
        mapObj.setDestination(end_address);
        var directions = mapObj.getDirections();
        var {getTheLeg, meters} = directions.routes.map(({legs: [{duration, distance}]}) => ({duration, distance})).reduce((o, {duration, distance}, i) => {
          if (i == 0 || o.meters > distance.value) {
            o.meters = distance.value;
            o.getTheLeg.duration.value = duration.value;
          }
          return o;
        }, {getTheLeg: {duration: {value: 0}}, meters: 0});
      

注:

  • 在这种情况下,请启用 Google Apps 脚本项目的 V8 运行时,如果您没有启用的话。

参考文献: