我需要找到从一个地方到另一个地方的估计行驶时间。我有两个地方的纬度和经度,但我不知道该怎么做。是否有任何API。帮助谢谢。
5 回答
是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似的方向细节。您从 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 地图服务的 Java 客户端,由于可能丢失 API 密钥(如PK Gupta 在评论中所述),该客户端不打算在 Android 应用程序中使用。因此,我不再推荐将其用于生产目的。
正如 Praktik 已经描述的那样,您可以使用 Google 的路线 API 来估计从一个地方到另一个地方所需的时间,同时考虑路线和交通情况。但是您不必使用 Web API 并构建自己的包装器,而是使用 Google 自己提供的Java 实现,该实现可通过Maven/gradle存储库获得。
将 google-maps-services 添加到应用的 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),也不会处理多个路由或分支。起点和终点也可以直接设置为LatLng
实例(参见DirectionsApiRequest#origin(LatLng)
和DirectionsApiRequest#destination(LatLng)
.
您也可以使用 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();