簡體   English   中英

如何獲取Android中兩個動態地理位置之間的預計到達時間?

[英]How to get the estimated time of arrival between two dynamic geolocations in Android?

有什么靈巧的方法(算法,API等)可以估算兩個地理位置之間的到達時間,而這些地理位置只不過是兩個人試圖在地圖上互相到達而已?

Google Maps Directions Api為您提供了一種獲取兩點之間旅行的估計旅行時間的方法,因此,如果您知道兩個用戶的位置,都可以利用。

例如,如果您發送https://maps.googleapis.com/maps/api/directions/json?origin=NewYork&destination=LosAngeles&mode=walking ,則響應的一部分將包括

    duration: {
        text: "37 days 23 hours",
        value: 3278737
    }

實現細節取決於您的應用程序的工作方式,但是api當然可以作為起點。

沒有直接解決您的問題的方法。

但是google提供了google direction api 此api使您能夠計算從一個位置到另一位置的距離,並為您提供估計時間,但有這些限制-

  1. 這是靜態的,不是動態的。 因此您無法真正計算必須提供兩個位置的移動人員,而這將給出與兩個位置的距離。
  2. 這給出了沿着道路的距離。 所以你不能真正定義自己的道路。 谷歌方向API會給出行駛道路的距離。
  3. 時間是平均旅行時間。 它不會考慮您旅行的速度。

因此,要實現您想要的目標,您將不得不自己做很多事情。 主要計算。

您需要的api是導航api。 但不幸的是,谷歌沒有將其提供給公眾使用。

希望這個答案對您有幫助。

正如RJ Aylward描述的那樣 ,您可以使用Google的Directions 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; } 

為簡單起見,此代碼不處理異常,錯誤情況(例如,未找到任何路由-> route.length == 0),也不會打擾多於一個routeleg 也可以將起點和終點直接設置為LatLng實例(請參見DirectionsApiRequest#origin(LatLng)DirectionsApiRequest#destination(LatLng)

進一步閱讀: android.jlelse.eu-Google Maps Directions API

這是我已經在Android上給出的答案-如何估算從一個地方到另一個地方的行駛時間?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM