1
我有一個GeoJSON數據,它給我所有的點(座標)。 使用這些點,我可以使用下面的代碼在地圖上繪製多段線。地圖框導航sdk使用地圖上的所有點(座標)輪流輪到
mapboxMap.addPolyline(new PolylineOptions()
.addAll(mapMatchedPoints)
.color(Color.GREEN)
.alpha(0.65f)
.width(4));
然後我想用這個
NavigationRoute.Builder navigationRoute = NavigationRoute.builder()
.accessToken(Mapbox.getAccessToken());
navigationRoute.origin(origin);
navigationRoute.destination(destination);
這裏需要起點和終點通過地圖上的所有點做導航。聽其回調方法給出了下面的代碼
navigationRoute.build().getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
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
route = response.body().getRoutes().get(0);
Log.d(TAG, "Distance: " + route.getDistance());
// Draw the route on the map
drawRoute(route, origin, destination);
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Log.e(TAG, "Error: " + t.getMessage());
}
});
drawroute 路線是方法,用於繪製導航路線
private void drawRoute(DirectionsRoute route, Position origin, Position destination) {
// Convert LineString coordinates into LatLng[]
LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6);
List<Position> coordinates = lineString.getCoordinates();
LatLng[] points = new LatLng[coordinates.size()];
for (int i = 0; i < coordinates.size(); i++) {
points[i] = new LatLng(
coordinates.get(i).getLatitude(),
coordinates.get(i).getLongitude());
}
// Draw Points on MapView
mapboxMap.addPolyline(new PolylineOptions()
.add(points)
.color(Color.parseColor("#3887be"))
.width(5));
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(origin.getLatitude(), origin.getLongitude()), 18));
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(origin.getLatitude(), origin.getLongitude())).setTitle("Origin"));
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(destination.getLatitude(), destination.getLongitude())).setTitle("Destination"));
}
但它不是通過所有的點來繪製。
它只是從原點到目的地繪圖,避免它們之間的中間點。
正如本屏幕截圖所示,它僅顯示從原點到目的地。
我想讓它顯示的路線通過綠色標記折線,並通過它導航。
我想它是計算點和繪圖線之間的最短路徑。
@所有幫助將不勝感激 – Kapil
您檢索的方向取決於您設置的配置文件。但是如果你想確保它通過一些特定的點,你需要添加這些作爲航點,當你做導航。路由電話。 –
謝謝@David Olsson,會嘗試更新你 – Kapil