所以我有一個小腳本,我用它來使用地圖查找位置之間的距離。大多數人都想使用最短的推薦路線,但我試圖找到最長的路線。這是當前腳本的開始:
function googlemaps (start_address, end_address) {
start_address = "Starbucks, 799 A St, Hayward, CA 94541";
end_address = "Hayward BART Station, 699 B St, Hayward, CA 94541";
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
Logger.log(directions["routes"]);
Logger.log(directions["routes"].length);
};
當我在谷歌地圖上查看上述地址的方向時,我可以看到有 3 條可能的路線,但是當我嘗試查找陣列的長度時,它只顯示 1。我不確定我錯過了什么
uj5u.com熱心網友回復:
修改點:
在您的情況下,您可以使用檢索其他模式
setAlternatives(true)。參考這也已經在評論中提到。為了檢索
the longest您所期望的,需要從回應值中檢索它。
當這些反映在你的腳本中時,下面的修改怎么樣?
修改后的腳本:
function googlemaps(start_address, end_address) {
start_address = "Starbucks, 799 A St, Hayward, CA 94541";
end_address = "Hayward BART Station, 699 B St, Hayward, CA 94541";
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
mapObj.setAlternatives(true); // Added
var directions = mapObj.getDirections();
var res = directions.routes.sort((a, b) => a.legs[0].distance.value > b.legs[0].distance.value ? -1 : 1)[0]; // Modified
console.log(res.legs[0].distance.value); // Modified
// console.log(res); // Here, you can see the object of `the longest`.
}
- 運行此腳本時,您可以檢索
the longestas的物件res。在此修改中,為了檢索 的物件the longest,我使用了sort.
參考:
- 設定替代品(使用替代品)
- 種類()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/526973.html
標籤:谷歌地图谷歌应用脚本
