Google Maps API - 检测何时越过状态线并计算每个状态下行进的距离 [英] Google Maps API - detect when crossing state lines and calculate distance travelled in each state

查看:94
本文介绍了Google Maps API - 检测何时越过状态线并计算每个状态下行进的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用google maps api来计算我公司车辆在一段时间内在各个州的行驶里程数。对于在此期间旅行的用户,用户将在地图上创建路线,并且该程序应该能够生成一些输出,例如:'马里兰州:100英里,弗吉尼亚州:500英里'等。

我很确定我可以通过发送 DirectionsService 请求并遍历 response.routes [i] .legs [i] .steps [i] .path 数组,但不幸的是,这需要发送 Geocoder 数千个 LatLng s,这很快使我超过了我的配额。是否有更好的方法可以解决这个问题?我只需要检测路由何时穿过状态线并将其分解为单独的路由,但是我不知道我会怎么做,而不是迭代路径数组。

解决方案

如果您的路径数组中有1000个元素,并且您正在进行二进制搜索(而不是迭代),则应该只需要12次调用LatLng来确定路径穿过边界的位置。



当你超过配额时,你会得到什么样的反馈?如果它是一个异常返回,那么只有当您收到配额回复时,您才可以等待3.5秒。


I am using google maps api to calculate the number of miles my company's vehicles travel in each state over a period of time. For trips taken over the period, the user will create routes on the map, and the program should be able to generate some output like: 'Maryland: 100 miles, Virginia: 500 miles", etc.

I'm pretty sure I can do it by sending a DirectionsService request and iterating over the response.routes[i].legs[i].steps[i].path array, but unfortunately this requires sending Geocoder requests on thousands of LatLngs, which quickly puts me over my quota. Is there a better way to go about this? I really only need to detect when the route crosses a state line and break it into separate routes, but I don't know how I would do that without iterating over the path array.

解决方案

If you have 1000 elements in your path array and you are binary searching (instead of iterating), it should only take 12 calls to LatLng to determine where the path crosses the border.

What sort of feedback do you get when you go over quota? If it's an exception return, you could always do the 3.5 second wait only when you receive the quota pushback.

这篇关于Google Maps API - 检测何时越过状态线并计算每个状态下行进的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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