Android的 - 如何获得估计驾驶时间,从一个地方到另一个地方?

问题描述 投票:10回答:5

我需要从一个地方到另一个地方找到估算驱动时间。我已经得到了两地的纬度和经度,但我不知道该怎么做。是否有任何API为。帮助表示感谢。

android geolocation distance android-location
5个回答
12
投票

是的,你得到的时间和距离值以及许多像方向细节驾车,步行等方式。你从谷歌的方向API服务得到了

检查我们的这个链接

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


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;

也请看到这一点,如果上面没有你的作品:

Calculate distance between two points in google maps V3


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


0
投票

弃用注意以下描述的解决方案是基于谷歌的Java客户端的谷歌地图服务,这是not intended to be used in an Android App由于对API密钥的丢失的可能性(由PK Gupta in the comments已注明)。因此,我将不再recommened其用于生产目的。


正如已经described by Praktik,您可以使用谷歌的API的方向来估计从一个地方到另一个拍摄方向和流量考虑所需要的时间。但你不必使用Web API,并建立自己的包装,而是使用由谷歌本身,这是可以通过Java implementation库提供的Maven/gradle

  1. Add the google-maps-services to your app's build.gradledependencies { 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; }

为简单起见,此代码不处理异常,错误的情况下(例如,没有找到路线 - > routes.length == 0),也不会与一个以上的routeleg打扰。出发地和目的地也可以直接设置为LatLng实例(见DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng)

延伸阅读:android.jlelse.eu - Google Maps Directions API


-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();
© www.soinside.com 2019 - 2024. All rights reserved.