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アクセス権を同じアクセス権で有効にした新しいアクセストークンとして使用します。私は間違って何をしていますか?
ありがとうございます。
重要な点です!すべての機能は、PositionをLongとLatで使用する場合、LatとLongを使用しています。 – Peter
これは私を救った! Position.fromCoordinatesメソッドは予期しない順序でLatitudeとLongitudeを使用します。経度と緯度は完全に規則違反となり、地図上に経路が描画されないというエラーは表示されません。 – serenskye