Android-如何获得从一个地方到另一个地方的预计驾驶时间?
我需要找到从一个地方到另一个地方的估计驾驶时间。我知道这两个地方的纬度和经度,但我不知道怎么做。是否有任何API用于此。Android-如何获得从一个地方到另一个地方的预计驾驶时间?,android,geolocation,distance,android-location,Android,Geolocation,Distance,Android Location,我需要找到从一个地方到另一个地方的估计驾驶时间。我知道这两个地方的纬度和经度,但我不知道怎么做。是否有任何API用于此。 帮助,谢谢。是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似方向的详细信息。您从google direction api服务获得的一切 查看我们的链接 编辑: //For example spead is 10 meters per minute. int speedIs10MetersPerMinute = 10; float esti
帮助,谢谢。是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似方向的详细信息。您从google direction api服务获得的一切 查看我们的链接 编辑:
//For example spead is 10 meters per minute.
int speedIs10MetersPerMinute = 10;
float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;
如果上述内容不适用于您,请参见此内容:
您也可以使用
{开始地址}&daddr={目的地址}
它将给出方向细节以及两个位置之间的距离和时间
弃用说明以下描述的解决方案基于谷歌地图服务的Java客户端,这是由于API密钥可能丢失(如所述)。因此,我不再建议将其用于生产目的
正如之前一样,您可以使用谷歌的方向API来估计从一个地方到另一个地方所需的时间,同时考虑方向和流量。但是您不必使用WebAPI并构建自己的包装器,而是使用Google自己提供的,可以通过存储库获得
// - 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;
}
LatLng
实例(请参见DirectionsApiRequest#起点(LatLng)
和DirectionsApiRequest#终点(LatLng)
)
进一步阅读:为什么不使用谷歌地图?我只需要估计的时间而不是方向……我想这将返回公里而不是时间这将返回米数的距离&这就是为什么我写道“现在你有米数的距离,这样你就可以计算估计的驾驶时间了。”你能告诉我如何获得时间吗?我不需要在两个地方之间导航的url。我需要本地变量中的估计旅行时间…请注意,这个答案给出的旅行时间忽略了物理路线本身和交通时间。谢谢你的回复。但这是android手机应用程序需要的javascript。你可以使用此服务在android中,您也只需要使用http Package调用服务。我在那里找不到估计的时间。您可以提供任何特定于时间的参考。请尝试在浏览器中执行此操作,并保存json文件,以查找持续时间。从单独的步骤持续时间以及总持续时间旅行中都可以找到。谢谢您的回复,但我会这样做在两个地方之间导航不需要url。我需要本地变量中的估计旅行时间…这不考虑道路。例如,它会告诉我可以从加利福尼亚开车到夏威夷。客户端不鼓励使用此java API。“Google Maps服务的Java客户端设计用于服务器应用程序。由于可能会丢失API密钥,此库不适用于Android应用程序内部使用。”这是我使用
directionsResult.routes[0]的最佳解决方案。legs[0].duration
获取最短的公路距离。不过,您需要在谷歌云中启用DirectionsApi
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();
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;
}