0

I want to draw a complete route on Mapbox from origin to destination using .geojson file.

Please find below code.

 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);
}`

Route is getting draw but from origin to destination. For example, if complete route is like, A-B-C-D-E, so I want to draw route which connects A-B-C-D-E but I am able to connect directly A-E. Please guide. Thanks in advance.

SaurabhG
  • 173
  • 1
  • 11
  • You are just asking MapboxDirections to create a route from A to E. You should include other points as waypoints in your Directions call in order to get a route that is alongside the other points of interest. – David Olsson Sep 22 '17 at 10:49
  • Yes I did that but multiple waypoints are not yet supported. and problem in using this is, - When we reach B waypoint from A waypoint, remaining distance shows as 0. - It can add only 25 waypoints (For Driving Profile), and file I have has 70-80 waypoints. – SaurabhG Sep 22 '17 at 12:18
  • Multiple waypoints exists in the MapboxDirections (are they missing in the sdk?) The official docs say up to 25 waypoints (not using driving-traffic) and 3 waypoints using driving-traffic – David Olsson Sep 22 '17 at 12:29
  • When we reach B waypoint from A waypoint, remaining distance shows as 0. and i have near about 70-80 waypoints. – SaurabhG Sep 22 '17 at 12:43
  • So either straight out decrease the amount of waypoints, or split them up to more requests. – David Olsson Sep 22 '17 at 13:30
  • I was also thinking about this but is this the only way to do this or is there any better way to do this?? – SaurabhG Sep 22 '17 at 16:43

0 Answers0