Android - 如何获取从一个地方到另一个地方的预计行驶时间?
我需要找到从一个地方到另一个地方的估计车程。我有两个地方的纬度和经度,但我不知道该怎么做。有没有相关的API。 帮助谢谢。
I need to find the estimate drive time from one place to another. I've got latitudes and longitudes for both places but I have no idea how to do that. Is there is any API for that.
help thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似方向的详细信息。您从 google Direction api 服务获得的所有信息
请查看我们的此链接
http://code.google .com/apis/maps/documentation/directions/
yes you get the time and distance value as well as many like direction details in driving, walking etc mode. all you got from the google direction api service
check our this links
http://code.google.com/apis/maps/documentation/directions/
编辑:
如果上述内容不适合您,另请参阅:
计算谷歌地图V3中两点之间的距离
EDIT :
Please also see this, if above not works for you:
Calculate distance between two points in google maps V3
弃用说明以下描述的解决方案基于 Google 的Google 地图服务 Java 客户端,即不打算在 Android 应用程序中使用,因为可能会丢失 API 密钥(如 PK 古普塔在评论中)。因此,我不再建议将其用于生产目的。
正如 Praktik 所描述的,您可以使用 Google 的路线 API 来估计从一个地方到另一个地方的路线所需的时间和流量考虑在内。但您不必使用 Web API 并构建自己的包装器,而是使用 由 Google 本身提供的 Java 实现,可通过 Maven/gradle 存储库。
将 google-maps-services 添加到您应用的 build.gradle:
执行请求并提取持续时间:
为了简单起见,此代码不处理异常、错误情况(例如,找不到路由 -> paths.length == 0),也不关心更多多于一条路线或腿。出发地和目的地也可以直接设置为
LatLng
实例(请参阅DirectionsApiRequest#origin(LatLng)
和DirectionsApiRequest#destination(LatLng)
。进一步阅读: android.jlelse.eu - Google 地图路线 API
Deprecation note The following described solution is based on Google's Java Client for Google Maps Services which is not intended to be used in an Android App due to the potential for loss of API keys (as noted by PK Gupta in the comments). Hence, I would no longer recommened it to use for production purposes.
As already described by Praktik, you can use Google's directions API to estimate the time needed to get from one place to another taking directions and traffic into account. But you don't have to use the web API and build your own wrapper, instead use the Java implementation provided by Google itself, which is available through the Maven/gradle repository.
Add the google-maps-services to your app's build.gradle:
Perform the request and extract the duration:
For simplicity, this code does not handle exceptions, error cases (e.g. no route found -> routes.length == 0), nor does it bother with more than one route or leg. Origin and destination could also be set directly as
LatLng
instances (seeDirectionsApiRequest#origin(LatLng)
andDirectionsApiRequest#destination(LatLng)
.Further reading: android.jlelse.eu - Google Maps Directions API
您还可以使用
http://maps.google.com/maps?saddr={start_address}&daddr ={destination_address}
它将给出方向详细信息以及两个位置之间的距离和时间
http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml
You can also use
http://maps.google.com/maps?saddr={start_address}&daddr={destination_address}
it will give in direction detail along with distance and time in between two locations
http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml