2016-04-06 7 views
-1

2つのジオロケーション間の到着時刻を推定するスマートな方法(アルゴリズム、APIなど)はありますか?これらのジオロケーションはマップ上でお互いに到達しようとしているだけです。Androidで2つの動的ジオロケーション間の到着予定時刻を取得するにはどうすればよいですか?

+1

可能な重複に与える答えです。 com/questions/8410787/android-how-to-get-estimated-drive-time-one-place-to-another) – Murmel

答えて

2

Google Maps Directions Apiは、2つの地点間の旅行の見積もり時間を取得する方法を提供します。したがって、両方のユーザーの場所を知っていれば、それを利用できます。例えば

あなたが送信した場合、応答のhttps://maps.googleapis.com/maps/api/directions/json?origin=NewYork&destination=LosAngeles&mode=walking一部が含まれます

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

実装の詳細は、アプリケーションがどのように機能するかに依存するが、APIは確かに出発点である可能性があります。

2

問題の直接的な解決策はありません。

ただし、Googleではgoogle direction apiを提供しています。このAPIは、あなたは 別 の場所に距離を計算することができますし、また、あなたの予想時間を与えますが、これらのlimitations-

  1. でこれは静的、動的ではありません。 2つの場所を指定する必要がある移動する人物を実際に計算することはできません。これにより、2つの場所からの距離が与えられます。
  2. これは道路に続く距離を示します。あなたは本当に自分のパスを定義することはできません。 Googleの方向apiは旅行のための距離を与えるでしょう。
  3. 時間は平均旅行時間です。あなたが旅行するスピードを考慮に入れません。

あなたが望むものを達成するためには、自分で多くのことをする必要があります。主な計算。

必要なapiはナビゲーションAPIです。残念ながら、これは一般にGoogleが提供しているものではありません。

この回答がお役に立てば幸いです。

0

すでにdescribed by RJ Aylwardと同じように、GoogleのDirections APIを使用して、ある場所から別の場所に移動して方向や交通量を考慮して必要な時間を見積もることができます。しかし、Web APIを使用して独自のラッパーを構築する必要はなく、Google自身が提供するJava implementationを使用してください。Maven/gradleリポジトリから利用できます。

  1. Add the google-maps-services to your app's 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)、またはrouteまたは複数で悩まない。起点および目的地は、LatLngのインスタンスとして直接設定することもできます(DirectionsApiRequest#origin(LatLng)およびDirectionsApiRequest#destination(LatLng)を参照)。// stackoverflowの: - :

さらに読書[別の場所から推定駆動時間を取得するためにどのようにアンドロイド?](HTTPS android.jlelse.eu - Google Maps Directions API

これは、私はすでにのAndroid - How to get estimated drive time from one place to another?

関連する問題