【发布时间】:2012-01-14 16:43:42
【问题描述】:
我需要找出从一个地方到另一个地方的估计行驶时间。我有两个地方的纬度和经度,但我不知道该怎么做。是否有任何API。 帮忙谢谢。
【问题讨论】:
-
为什么不用谷歌地图?
-
我只需要估计时间而不是方向......
标签: android geolocation distance android-location
我需要找出从一个地方到另一个地方的估计行驶时间。我有两个地方的纬度和经度,但我不知道该怎么做。是否有任何API。 帮忙谢谢。
【问题讨论】:
标签: android geolocation distance android-location
是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似的方向详细信息。您从 google direction api 服务中获得的一切
查看我们的链接
【讨论】:
Location location1 = new Location("");
location1.setLatitude(lat);
location1.setLongitude(long);
Location location2 = new Location("");
location2.setLatitude(lat);
location2.setLongitude(long);
float distanceInMeters = location1.distanceTo(location2);
编辑:
//For example spead is 10 meters per minute.
int speedIs10MetersPerMinute = 10;
float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;
如果上述方法不适合您,请同时查看:
【讨论】:
弃用说明以下描述的解决方案基于 Google 的 Google Maps Services 的 Java 客户端,它是 not intended to be used in an Android App,因为可能会丢失 API 密钥(如上所述由PK Gupta in the comments)。因此,我不再推荐将其用于生产目的。
与described by Praktik 一样,您可以使用 Google 的路线 API 来估算从一个地方到另一个地方所需的时间,同时考虑路线和交通情况。但是您不必使用 Web API 并构建自己的包装器,而是使用 Google 自己提供的 Java implementation,它可以通过 Maven/gradle 存储库获得。
Add the google-maps-services to your app's build.gradle:
dependencies {
compile 'com.google.maps:google-maps-services:0.2.5'
}
执行请求并提取持续时间:
// - Put your api key (https://developers.google.com/maps/documentation/directions/get-api-key) here:
private static final String API_KEY = "AZ.."
/**
Use Google's directions api to calculate the estimated time needed to
drive from origin to destination by car.
@param origin The address/coordinates of the origin (see {@link DirectionsApiRequest#origin(String)} for more information on how to format the input)
@param destination The address/coordinates of the destination (see {@link DirectionsApiRequest#destination(String)} for more information on how to format the input)
@return The estimated time needed to travel human-friendly formatted
*/
public String getDurationForRoute(String origin, String destination)
// - We need a context to access the API
GeoApiContext geoApiContext = new GeoApiContext.Builder()
.apiKey(apiKey)
.build();
// - Perform the actual request
DirectionsResult directionsResult = DirectionsApi.newRequest(geoApiContext)
.mode(TravelMode.DRIVING)
.origin(origin)
.destination(destination)
.await();
// - Parse the result
DirectionsRoute route = directionsResult.routes[0];
DirectionsLeg leg = route.legs[0];
Duration duration = leg.duration;
return duration.humanReadable;
}
为简单起见,此代码不处理异常、错误情况(例如,找不到路由 -> routes.length == 0),也不会处理多个route 或leg。起点和终点也可以直接设置为LatLng 实例(参见DirectionsApiRequest#origin(LatLng) 和DirectionsApiRequest#destination(LatLng)。
【讨论】:
directionsResult.routes[0].legs[0].duration 获得公路最短距离。您需要在 Google 云中启用 DirectionsApi
你也可以使用 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
【讨论】:
Calculate Distance:-
float distance;
Location locationA=new Location("A");
locationA.setLatitude(lat);
locationA.setLongitude(lng);
Location locationB = new Location("B");
locationB.setLatitude(lat);
locationB.setLongitude(lng);
distance = locationA.distanceTo(locationB)/1000;
LatLng From = new LatLng(lat,lng);
LatLng To = new LatLng(lat,lng);
Calculate Time:-
int speedIs1KmMinute = 100;
float estimatedDriveTimeInMinutes = distance / speedIs1KmMinute;
Toast.makeText(this,String.valueOf(distance+
"Km"),Toast.LENGTH_SHORT).show();
Toast.makeText(this,String.valueOf(estimatedDriveTimeInMinutes+" Time"),Toast.LENGTH_SHORT).show();
【讨论】: