11

我需要找到从一个地方到另一个地方的估计行驶时间。我有两个地方的纬度和经度,但我不知道该怎么做。是否有任何API。帮助谢谢。

4

5 回答 5

13

是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似的方向细节。您从 google direction api 服务中获得的一切

检查我们的这个链接

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

于 2011-12-07T05:28:53.443 回答
11
    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中两点之间的距离

于 2011-12-07T05:36:05.227 回答
2

弃用说明以下描述的解决方案基于 Google 的用于 Google 地图服务的 Java 客户端,由于可能丢失 API 密钥(如PK Gupta 在评论中所述),该客户端不打算在 Android 应用程序中使用。因此,我不再推荐将其用于生产目的。


正如 Praktik 已经描述的那样,您可以使用 Google 的路线 API 来估计从一个地方到另一个地方所需的时间,同时考虑路线和交通情况。但是您不必使用 Web API 并构建自己的包装器,而是使用 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;
    }
    

为简单起见,此代码不处理异常、错误情况(例如,找不到路由 -> routes.length == 0),也不会处理多个路由分支。起点和终点也可以直接设置为LatLng实例(参见DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng).

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

于 2017-11-20T11:06:53.443 回答
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

于 2011-12-07T05:51:39.383 回答
-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();
于 2017-03-28T12:21:51.847 回答