我想使用 .geojson 文件在 Mapbox 上绘制从起点到终点的完整路线。
请找到下面的代码。
private void drawMapMatched(Position[] coordinates) {
try {
MapboxMapMatching client = new MapboxMapMatching.Builder()
.setAccessToken(Mapbox.getAccessToken())
.setSteps(true)
.setProfile(MapMatchingCriteria.PROFILE_DRIVING)
.setCoordinates(coordinates)
.build();
client.enqueueCall(new Callback<MapMatchingResponse>() {
@Override
public void onResponse(Call<MapMatchingResponse> call, Response<MapMatchingResponse> response) {
List<LatLng> mapMatchedPoints = new ArrayList<>();
if (response.code() == 200) {
String geometry = response.body().getMatchings().get(0).getGeometry();
List<Position> positions = PolylineUtils.decode(geometry, Constants.PRECISION_6);
if (positions == null) {
return;
}
for (int i = 0; i < positions.size(); i++) {
mapMatchedPoints.add(new LatLng(
positions.get(i).getLatitude(),
positions.get(i).getLongitude()));
}
if (mapMatchedRoute != null) {
mapboxMap.removeAnnotation(mapMatchedRoute);
}
mapMatchedRoute = mapboxMap.addPolyline(new PolylineOptions()
.color(Color.GREEN)
.alpha(0.65f)
.width(4));
for (int i = 0; i < mapMatchedPoints.size(); i++) {
mapMatchedRoute.addPoint(mapMatchedPoints.get(i));
}
Position origin = Position.fromCoordinates(mapMatchedPoints.get(0).getLongitude(), mapMatchedPoints.get(0).getLatitude());
Position destination = Position.fromCoordinates(mapMatchedPoints.get(mapMatchedPoints.size() - 1).getLongitude(), mapMatchedPoints.get(mapMatchedPoints.size() - 1).getLatitude());
getRoute(origin, destination);
} else {
Log.e(TAG, "Too many coordinates, profile not found, invalid input, or no match.");
}
}
@Override
public void onFailure(Call<MapMatchingResponse> call, Throwable throwable) {
Log.e(TAG, "MapboxMapMatching error: " + throwable.getMessage());
}
});
} catch (ServicesException servicesException) {
servicesException.printStackTrace();
}
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, null); locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NAVIGATION);
}
private void getRoute(final Position origin, final Position destination) {
ArrayList<Position> positions = new ArrayList<>();
positions.add(origin);
positions.add(destination);
MapboxDirections client = new MapboxDirections.Builder()
.setAccessToken(Mapbox.getAccessToken())
.setOrigin(origin)
.setDestination(destination)
.setAlternatives(true)
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setSteps(true)
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
.setBearings(new double[]{60, 45}, new double[]{45, 45})
.setAnnotation(DirectionsCriteria.ANNOTATION_DISTANCE, DirectionsCriteria.ANNOTATION_DURATION)
.build();
client.enqueueCall(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
Log.d(TAG, "API call URL: " + call.request().url().toString());
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
route = response.body().getRoutes().get(0);
//showMessage(String.format(Locale.US, "Route is %.1f meters long.", currentRoute.getDistance()));
// Draw the route on the map
drawRoute(route, origin, destination);
}
@Override
public void onFailure(Call<MapMatchingResponse> call, Throwable throwable) {
}
});
} catch (ServicesException servicesException) {
servicesException.printStackTrace();
}
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, null);
locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NAVIGATION);
}`
路线正在绘制,但从起点到目的地。例如,如果完整的路线是 ABCDE,那么我想绘制连接 ABCDE 的路线,但我可以直接连接 AE。请指导。提前致谢。