我需要找到从一个地方到另一个地方的估计开车时间。我有两个地方的纬度和经度,但我不知道该怎么做。是否有任何API。 帮助谢谢。
答案 0 :(得分:11)
是的,您可以获得时间和距离值以及驾驶,步行等模式中的许多方向细节。所有你从谷歌方向API服务
查看我们的链接
答案 1 :(得分:9)
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;
如果上述内容不适用于您,请同时查看:
答案 2 :(得分:1)
您也可以使用 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
答案 3 :(得分:0)
弃用说明以下所述解决方案基于Google的
已经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;
}
为简单起见,此代码不处理异常,错误情况(例如找不到路由 - > gt; routes.length == 0),也不会处理多个route或leg 。原点和目的地也可以直接设置为LatLng
个实例(请参阅DirectionsApiRequest#origin(LatLng)
和DirectionsApiRequest#destination(LatLng)
。
答案 4 :(得分:-2)
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();