Android – 如何获得从一个地方到另一个地方的预计驾驶时间?

我需要find从一个地方到另一个地方的估算驾驶时间。 我对这两个地方都有经纬度,但是我不知道该怎么做。 是否有任何API。 谢谢。

Solutions Collecting From Web of "Android – 如何获得从一个地方到另一个地方的预计驾驶时间?"

是的,你得到的时间和距离的价值,以及很多方向的细节在驾驶,步行等模式。 所有你从谷歌方向API服务

检查我们这个链接

http://code.google.com/apis/maps/documentation/directions/

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; 

请也看到这个,如果上面不适合你:

计算谷歌地图V3中两点之间的距离

您还可以使用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

正如Praktik所描述的那样 ,您可以使用Google的路线API来估算从一个地方到另一个地方所需的时间,并将方向和stream量考虑在内。 但是,您不必使用Web API并构build自己的包装器,而是使用Google自己提供的Java实现 ,它可以通过Maven / gradle存储库获得。

  1. 将google-maps-services添加到您应用的build.gradle :

     dependencies { compile 'com.google.maps:google-maps-services:0.2.5' } 
  2. 执行请求并提取持续时间:

     // - 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; } 

为了简单起见,这段代码不处理exception,错误情况(例如,没有find路由 – > routes.length == 0),也不会打扰多个路由或路段 。 起点和终点也可以直接设置为LatLng实例(请参阅DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng)

进一步阅读: android.jlelse.eu – Google Maps Directions API

  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();