2016-05-25 11 views
2

MapBox Directionsをアンドロイドで使用しており、地図上に描画するための道順を取得しようとしています。Mapbox Directions APIがステータスコード422で無効な応答を返す

しかし、response.body()がnullを返すため、呼び出しから取得した応答は使用できません。

私がresponse.code()を呼び出すと、Mapbox APIによると、「指定されたリクエストは無効です。」というステータスコードが返されます。応答のメッセージキーに無効な入力"

response.message()は、文字列 "不明"を返します。

response.errorBody()文字列を返す「[email protected]

私は動作するはずthis stack overflow answerと同じコードを使用しているが、それはしていません。どんな助けもありがとう。

HERESに私の方法:

private void getRoute(Position origin, Position destination) throws ServicesException { 

    MapboxDirections client = new MapboxDirections.Builder() 
      .setOrigin(origin) 
      .setDestination(destination) 
      .setProfile(DirectionsCriteria.PROFILE_CYCLING) 
      .setAccessToken("Same access token as the map uses") 
      .build(); 

    client.enqueueCall(new Callback<DirectionsResponse>() { 
     @Override 
     public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { 
      // You can get the generic HTTP info about the response 
      Log.d(TAG, "Response code: " + response.code()); 
      if (response.body() == null) { 
       Log.e(TAG, "No routes found, make sure you set the right user and access token."); 
       return; 
      } 

      // Print some info about the route 
      currentRoute = response.body().getRoutes().get(0); 
      Log.d(TAG, "Distance: " + currentRoute.getDistance()); 
      Toast.makeText(MainActivity.this, "Route is " + currentRoute.getDistance() + " meters long.", Toast.LENGTH_SHORT).show(); 

      // Draw the route on the map 
      drawRoute(currentRoute); 
     } 

     @Override 
     public void onFailure(Call<DirectionsResponse> call, Throwable t) { 
      Log.e(TAG, "Error: " + t.getMessage()); 
      Toast.makeText(MainActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

そして、私の輸入:

import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 
import com.mapbox.services.directions.v5.DirectionsCriteria; 
import com.mapbox.services.directions.v5.MapboxDirections; 
import com.mapbox.services.directions.v5.models.DirectionsResponse; 
import com.mapbox.services.directions.v5.models.DirectionsRoute; 

そして、私のGradle:私もと異なる国の異なる位置で試してみました

compile ('com.mapbox.mapboxsdk:mapbox-android-services:[email protected]'){ 
    transitive=true 
} 

assアクセス権を同じアクセス権で有効にした新しいアクセストークンとして使用します。私は間違って何をしていますか?

ありがとうございます。

答えて

1

1.0.0のように見えますが、方向V5に安定性をもたらす1.1.0に更新されました。更新によって問題が解決されない場合は、原点/目的地の座標を間違った順序で渡している可能性があります。経度、緯度です。緯度、経度ではありません。最後に、これで問題が解決しない場合は、実際のURLリクエストを見てみましょう。ログに記録して質問を編集できますか?

6

Positionオブジェクトの作成時に正しい方法で緯度と経度を設定しているかどうかを確認してください。

コンストラクタは重要です。

Position pos = Position.fromCoordinates(/*LON FIRST*/lon, /*LAT SECOND*/ lat); 

例:

Position origin = Position.fromCoordinates(location.getLongitude(),location.getLatitude()); 

希望あなたや将来のコンサルタントにこのヘルプ。

+1

重要な点です!すべての機能は、PositionをLongとLatで使用する場合、LatとLongを使用しています。 – Peter

+0

これは私を救った! Position.fromCoordinatesメソッドは予期しない順序でLatitudeとLongitudeを使用します。経度と緯度は完全に規則違反となり、地図上に経路が描画されないというエラーは表示されません。 – serenskye

関連する問題