计算两个地址之间的距离时出现错误 [英] Errors When Calculating Distance Between Two Addresses

查看:52
本文介绍了计算两个地址之间的距离时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电子表格中使用以下脚本来计算两个城市或一个城市之间的行驶距离以及另一个城市的邮政编码.它同时在大约25个位置运行.为了更好地说明,我拥有B3单元格,每次我都会在其中进入一个新城市.然后,将脚本用于与我的25个工厂位置相邻的单元中,以计算从我的每个工厂到可变城市的距离.

它使用内置在Google Maps API中的Google表格,并且可以进行80%的计算,但是会返回"TypeError:无法读取未定义的属性" legs"(第16行).失败的工厂随每个新城市而变化因此,它与某些位置不一样.这几乎就像api在完成其中一些操作之前就超时了一样.我将其拆分为两个具有不同名称的单独脚本,这些脚本工作了一天,但随后有20%再次失败. /p>

为了使事情更奇怪,我还有另一个脚本,该脚本根据到变量地址的最接近距离对植物进行排序.当您对植物进行分类时,即使是有错误的植物也会根据距离而到达正确的位置.因此,就像距离脚本正在获取正确的距离,但无论如何都会显示错误.

像泥一样清晰吗?希望我能提供有关如何解决问题的任何意见,也可以使用能够解决我的问题的替代映射API.

function distancecalcone(origin,destination) {

  var directions = Maps.newDirectionFinder()

    //Set the Method of Transporation. The available "modes" are WALKING,    DRIVING, BICYCLING, TRANSIT.
    .setMode(Maps.DirectionFinder.Mode.DRIVING)

    //Set the Orgin
    .setOrigin(origin)

    //Set the Destination
    .setDestination(destination)

    //Retrieve the Distance
    .getDirections();
    return directions.routes[0].legs[0].distance.value/1609.34;
}

解决方案

您是否尝试过在Directions.routes [0] .legs [0] .distance.value吗?附近使用try-catch块?

try{
    return directions.routes[0].legs[0].distance.value/1609.34;
} 
catch (e){
 console.log("error",e)
}

或者您可以尝试这样的事情

alert(directions);
alert(directions.routes[0]);
alert(directions.routes[0].legs[0]);
alert(directions.routes[0].legs[0].distance);
alert(directions.routes[0].legs[0].distance.value);

,依此类推...找出第一个未定义的出现.这可能有助于您调试问题.

I have the following script which is being used in a spreadsheet to calculate the driving distance between two cities or a city and a zip code of another city. It is being run for approximately 25 locations simultaneously. To better explain, I have cell B3 in which I enter a new city every time. The script is then used in cells adjacent to my 25 plant locations to calculate the distance from each of my plants to the variable city.

It uses google sheets built in mapping api and works on 80% of the calculations but returns "TypeError: Can Not Read Property "legs" from undefined. (line 16). The plants that it fails on vary with every new city so its not like it is for certain locations. It is almost like the api times out before it completes some of them. I split it into two separate scripts with a varied name and that worked for a day but then 20% fail again.

To make things slightly more odd, I have another script that sorts the plants based on closest distance to the variable address. When you sort the plants, even the ones with errors go to their correct location based on distance. So it is like the distance script is obtaining the correct disance but displaying the error anyways.

Clear as mud? Would love any input I could get on how to correct the issue or an alternate mapping api that could solve my problems.

function distancecalcone(origin,destination) {

  var directions = Maps.newDirectionFinder()

    //Set the Method of Transporation. The available "modes" are WALKING,    DRIVING, BICYCLING, TRANSIT.
    .setMode(Maps.DirectionFinder.Mode.DRIVING)

    //Set the Orgin
    .setOrigin(origin)

    //Set the Destination
    .setDestination(destination)

    //Retrieve the Distance
    .getDirections();
    return directions.routes[0].legs[0].distance.value/1609.34;
}

解决方案

Have you tried using a try-catch block around directions.routes[0].legs[0].distance.value ?

try{
    return directions.routes[0].legs[0].distance.value/1609.34;
} 
catch (e){
 console.log("error",e)
}

or you could try something like this

alert(directions);
alert(directions.routes[0]);
alert(directions.routes[0].legs[0]);
alert(directions.routes[0].legs[0].distance);
alert(directions.routes[0].legs[0].distance.value);

and so on...to find out which one comes up as undefined the first. That might help you to debug the issue.

这篇关于计算两个地址之间的距离时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆