Android - 如何获取从一个地方到另一个地方的预计行驶时间?

发布于 2024-12-20 01:55:15 字数 68 浏览 3 评论 0原文

我需要找到从一个地方到另一个地方的估计车程。我有两个地方的纬度和经度,但我不知道该怎么做。有没有相关的API。 帮助谢谢。

I need to find the estimate drive time from one place to another. I've got latitudes and longitudes for both places but I have no idea how to do that. Is there is any API for that.
help thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

徒留西风 2024-12-27 01:55:15

是的,您可以在驾驶、步行等模式下获得时间和距离值以及许多类似方向的详细信息。您从 google Direction api 服务获得的所有信息

请查看我们的此链接

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

yes you get the time and distance value as well as many like direction details in driving, walking etc mode. all you got from the google direction api service

check our this links

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

悟红尘 2024-12-27 01:55:15
    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中两点之间的距离

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

EDIT :

    //For example spead is 10 meters per minute.
    int speedIs10MetersPerMinute = 10;
    float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;

Please also see this, if above not works for you:

Calculate distance between two points in google maps V3

〃安静 2024-12-27 01:55:15

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


正如 Praktik 所描述的,您可以使用 Google 的路线 API 来估计从一个地方到另一个地方的路线所需的时间和流量考虑在内。但您不必使用 Web API 并构建自己的包装器,而是使用 由 Google 本身提供的 Java 实现,可通过 Maven/gradle 存储库。

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

    依赖项{
        编译 'com.google.maps:google-maps-services:0.2.5'
    }
    
  2. 执行请求并提取持续时间:

    // - 将您的 api 密钥 (https://developers.google.com/maps/documentation/directions/get-api-key) 放在这里:
    私有静态最终字符串 API_KEY = "AZ.."
    
    /**
    使用 Google 的路线 api 计算到达所需的预计时间
    开车从出发地到目的地。
    
    @param origin 原点的地址/坐标(有关如何格式化输入的更多信息,请参阅{@link DirectionsApiRequest#origin(String)})
    @param destination 目的地的地址/坐标(有关如何格式化输入的更多信息,请参阅 {@link DirectionsApiRequest#destination(String)})
    
    @return 出行所需的预计时间 人性化格式
    */
    公共字符串 getDurationForRoute(字符串起点,字符串目的地)
        // - 我们需要一个上下文来访问 API 
        GeoApiContext geoApiContext = new GeoApiContext.Builder()
            .apiKey(apiKey)
            。建造();
    
        // - 执行实际请求
        DirectionsResult DirectionsResult = DirectionsApi.newRequest(geoApiContext)
                .mode(旅行模式.驾驶)
                .origin(原点)
                .目的地(目的地)
                。等待();
    
        // - 解析结果
        DirectionsRoute 路线 = DirectionsResult.routes[0];
        DirectionsLeg 腿 = 路线.legs[0];
        持续时间持续时间=leg.duration;
        返回持续时间. humanReadable;
    }
    

为了简单起见,此代码不处理异常、错误情况(例如,找不到路由 -> paths.length == 0),也不关心更多多于一条路线。出发地和目的地也可以直接设置为 LatLng 实例(请参阅 DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng)

进一步阅读: android.jlelse.eu - Google 地图路线 API

Deprecation note The following described solution is based on Google's Java Client for Google Maps Services which is not intended to be used in an Android App due to the potential for loss of API keys (as noted by PK Gupta in the comments). Hence, I would no longer recommened it to use for production purposes.


As already described by Praktik, you can use Google's directions API to estimate the time needed to get from one place to another taking directions and traffic into account. But you don't have to use the web API and build your own wrapper, instead use the Java implementation provided by Google itself, which is available through the Maven/gradle repository.

  1. Add the google-maps-services to your app's build.gradle:

    dependencies {
        compile 'com.google.maps:google-maps-services:0.2.5'
    }
    
  2. Perform the request and extract the duration:

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

For simplicity, this code does not handle exceptions, error cases (e.g. no route found -> routes.length == 0), nor does it bother with more than one route or leg. Origin and destination could also be set directly as LatLng instances (see DirectionsApiRequest#origin(LatLng) and DirectionsApiRequest#destination(LatLng).

Further reading: android.jlelse.eu - Google Maps Directions API

时间你老了 2024-12-27 01:55:15

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

You can also use
http://maps.google.com/maps?saddr={start_address}&daddr={destination_address}

it will give in direction detail along with distance and time in between two locations

http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml

漫漫岁月 2024-12-27 01:55:15
   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();
   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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文