0
votes

I am using MapBox to create a route between two points. I have LatLng of two points already and have successfully add Markers at source and destination point but the problem is, I am unable to create a route between them. Here is the code. I don't know where i am lacking but i have followed a tutorial but in that Tutor is creating a route between current location and the location clicked on map and he is doing stuff in onMapClicked method since i am not using that approach due to have customized point. Instead, i am doing stuff in the onMapReady method other than all the procedure is identical.

package com.devaj.googlemaps;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.devaj.googlemaps.models.TDD;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;

public class NavigationActivity extends AppCompatActivity implements OnMapReadyCallback{

    public static final String TAG = "NavigationActivity";
    private MapView mapView;
    private TDD tdd;
    private Point originPosition, dstPosition;
    private Double originLat, originLng, dstLat, dstLng;
    private LatLng originLatLng, dstLatLng;
    private MapboxMap map;
    private NavigationMapRoute navigationMapRoute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this, getString(R.string.access_token));
        setContentView(R.layout.activity_navigation);
        MapBox(savedInstanceState);
    }

    @Override
    public void onMapReady(@NonNull MapboxMap mapboxMap) {
        map = mapboxMap;
        originPosition = Point.fromLngLat(originLat, originLng);
        dstPosition = Point.fromLngLat(dstLat, dstLng);

        MarkerOptions options = new MarkerOptions();
        options.title("Source");
        options.position(originLatLng);

        MarkerOptions options1 = new MarkerOptions();
        options1.title("Destination");
        options1.position(dstLatLng);

        mapboxMap.addMarker(options);
        mapboxMap.addMarker(options1);

        mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {
                navigationMapRoute = new NavigationMapRoute(null, mapView, map, R.style.NavigationMapRoute);
                getRoute(originPosition, dstPosition);
            }
        });


    }

    private void MapBox(Bundle savedInstanceState)
    {
        Intent i = getIntent();
        tdd = (TDD) i.getSerializableExtra("tdd");

        originLat = Double.parseDouble(tdd.getmSrcLat());
        originLng = Double.parseDouble(tdd.getmSrcLng());
        dstLat = Double.parseDouble(tdd.getmDstLat());
        dstLng = Double.parseDouble(tdd.getmDstLng());

        originLatLng = new LatLng(originLat, originLng);
        dstLatLng = new LatLng(dstLat, dstLng);

        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
    }

    private void getRoute(Point origin, Point destination)
    {

//        CameraPosition position = new CameraPosition.Builder()
//                .target(originLatLng) // Sets the new camera position
//                .zoom(17) // Sets the zoom
//                .bearing(180) // Rotate the camera
//                .tilt(30) // Set the camera tilt
//                .build(); // Creates a CameraPosition from the builder
//
//        map.animateCamera(CameraUpdateFactory
//                .newCameraPosition(position), 7000);


        NavigationRoute.builder(NavigationActivity.this)
                .accessToken(Mapbox.getAccessToken())
                .origin(origin)
                .destination(destination)
                .build()
                .getRoute(new Callback<DirectionsResponse>() {
                    @Override
                    public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                        if (response.body() == null)
                        {
                            Usable.logMessage(TAG, "No routes found, Check User and Access Token..");
                            return;
                        } else if (response.body().routes().size() == 0)
                        {
                            Usable.logMessage(TAG, "No routes found..");
                            return;
                        }


                        DirectionsRoute currentRoute = response.body().routes().get(0);
                        navigationMapRoute.addRoute(currentRoute);
                    }

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

    }

    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

}
1

1 Answers

0
votes

You've got your lat/long order mixed up whenever you use Point.fromLngLat().

For example, dstPosition = Point.fromLngLat(dstLat, dstLng); is the wrong order.

Is there a log message in your logcat when you try the code you posted above? Search for Mbgl. Is response.body() null? Is the route size == 0? Basically, it'd be helpful if you could explain more when you say I am unable to create a route between them.

By the way, you don't have to use the Mapbox Navigation SDK for Android if you want to get a route between two points. The Mapbox Java SDK can do that.

Also, I'd move the onMapReady() stuff into the onStyleLoaded() callback.

@Override
  public void onMapReady(@NonNull MapboxMap mapboxMap) {
    mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
      @Override
      public void onStyleLoaded(@NonNull Style style) {
        map = mapboxMap;
        originPosition = Point.fromLngLat(originLng, originLat);
        dstPosition = Point.fromLngLat(dstLng, dstLat);

        MarkerOptions options = new MarkerOptions();
        options.title("Source");
        options.position(originLatLng);

        MarkerOptions options1 = new MarkerOptions();
        options1.title("Destination");
        options1.position(dstLatLng);

        mapboxMap.addMarker(options);
        mapboxMap.addMarker(options1);

        navigationMapRoute = new NavigationMapRoute(null, mapView, map, R.style.NavigationMapRoute);
        getRoute(originPosition, dstPosition);
      }
    });
  }