mapbox / mapbox-navigation-android

Mapbox Navigation SDK for Android
https://docs.mapbox.com/android/navigation/overview/
Other
622 stars 318 forks source link

Navigation API diplay white screen #474

Closed steven-haddad closed 6 years ago

steven-haddad commented 6 years ago

I am beginner on Mapbox.

I'm trying to calculate a trip with multiple point. My app calculated the best trip on the map view, but when i'm trying to display the trip on navigation API, the API display me white screen. However i debug my app and every seems to be fine .

To lauch the navigation API i called this function: NavigationLauncher.startNavigation(test3.this, optimizedRoute, null, false) I passed the route 'optimizedRoute' on parameter

Thanks for the help

``

private MapView mapView;
private DirectionsRoute optimizedRoute;
private MapboxOptimizedTrips optimizedClient;
private Polyline optimizedPolyline;
private List<Position> stops;
private Position origin;
private static final String FIRST = "first";
private static final String ANY = "any";
private static final String TEAL_COLOR = "#23D2BE";
private static final int POLYLINE_WIDTH = 5;
private MapboxMap map;
private PermissionsManager permissionsManager;
private LocationLayerPlugin locationPlugin;
private LocationEngine locationEngine;
private Marker destinationMarker;
private LatLng originCoord;
private LatLng destinationCoord;
private Location originLocation;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Mapbox access token is configured here. This needs to be called either in your application
    // object or in the same activity which contains the mapview.
    Mapbox.getInstance(this, getString(R.string.access_token));

    // This contains the MapView in XML and needs to be called after the access token is configured.
    setContentView(R.layout.activity_test);

    // Initialize list of Position objects and add the origin Position to the list
    initializeListOfStops();

    // Setup the MapView
    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            map = mapboxMap;

            // Add origin and destination to the map
            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(origin.getLatitude(), origin.getLongitude()))
                    .title("Nouveau marker"));

            map.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
                @Override
                public void onMapClick(@NonNull LatLng point) {
                    // Optimization API is limited to 12 coordinate sets
                    if (alreadyTwelveMarkersOnMap()) {
                        Toast.makeText(test3.this, "deja 12 markers", Toast.LENGTH_LONG).show();
                    } else {
                        addDestinationMarker(point);
                        addPointToStopsList(point);

                        getOptimizedRoute(stops);
                        button.setEnabled(true);
                        button.setBackgroundResource(R.color.mapboxBlue);
                        DirectionsRoute op = optimizedRoute;
                        Log.i("test","t"+optimizedRoute);

                    }
                }
            });

            enableLocationPlugin();
            button = findViewById(R.id.startButton);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    // Pass in your Amazon Polly pool id for speech synthesis using Amazon Polly
                    // Set to null to use the default Android speech synthesizer
                    String awsPoolId = null;

                    boolean simulateRoute = true;

                    NavigationLauncher.startNavigation(test3.this, optimizedRoute, null, false);

                    DirectionsRoute  op=optimizedRoute;

                    Log.i("te","d"+op);

                }
            });
        }
    });
}

private boolean alreadyTwelveMarkersOnMap() {
    if (stops.size() == 12) {
        return true;
    } else {
        return false;
    }
}

private void addDestinationMarker(LatLng point) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(point.getLatitude(), point.getLongitude()))
            .title("ajout d'un marker"));
}

private void addPointToStopsList(LatLng point) {
    stops.add(Position.fromCoordinates(point.getLongitude(), point.getLatitude()));
}

private void initializeListOfStops() {
    // Set up stop list
    stops = new ArrayList<>();
    // Set first stop
    origin = Position.fromCoordinates(2.3773043, 48.845496700000005);
    stops.add(origin);
}

private DirectionsRoute getOptimizedRoute(List<Position> coordinates) {
    optimizedClient = new MapboxOptimizedTrips.Builder()
            .setSource(FIRST)
            .setDestination(ANY)
            .setCoordinates(coordinates)
            .setOverview(DirectionsCriteria.OVERVIEW_FULL)
            .setProfile(DirectionsCriteria.PROFILE_DRIVING)
            .setAccessToken(Mapbox.getAccessToken())
            .build();

    optimizedClient.enqueueCall(new Callback<OptimizedTripsResponse>() {
        @Override
        public void onResponse(Call<OptimizedTripsResponse> call, Response<OptimizedTripsResponse> response) {
            if (!response.isSuccessful()) {
                Log.d("DirectionsActivity", "no succes");
                Toast.makeText(test3.this, 't', Toast.LENGTH_SHORT).show();
                return;
            } else {
                if (response.body().getTrips().isEmpty()) {
                    Log.d("DirectionsActivity", "impossible de detrminer l'itineraire" + " size = "
                            + response.body().getTrips().size());
                    Toast.makeText(test3.this, "route ok",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            // Get most optimized route from API response
            optimizedRoute = response.body().getTrips().get(0);
            drawOptimizedRoute(optimizedRoute);
        }

        @Override
        public void onFailure(Call<OptimizedTripsResponse> call, Throwable throwable) {
            Log.d("DirectionsActivity", "Error: " + throwable.getMessage());
        }
    });
    return null;
}

private void drawOptimizedRoute(DirectionsRoute route) {
    // Remove old polyline
    if (optimizedPolyline != null) {
        map.removePolyline(optimizedPolyline);
    }
    // Draw points on MapView
    LatLng[] pointsToDraw = convertLineStringToLatLng(route);
    optimizedPolyline = map.addPolyline(new PolylineOptions()
            .add(pointsToDraw)
            .color(Color.parseColor(TEAL_COLOR))
            .width(POLYLINE_WIDTH));
}

private LatLng[] convertLineStringToLatLng(DirectionsRoute route) {
    // Convert LineString coordinates into LatLng[]
    LineString lineString = LineString.fromPolyline(route.getGeometry(), 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());
    }
    return points;
}

@SuppressWarnings( {"MissingPermission"})
private void enableLocationPlugin() {
    // Check if permissions are enabled and if not request
    if (PermissionsManager.areLocationPermissionsGranted(this)) {
        // Create an instance of LOST location engine
        initializeLocationEngine();

        locationPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
        locationPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING);
    } else {
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}

@SuppressWarnings( {"MissingPermission"})
private void initializeLocationEngine() {
    locationEngine = new LostLocationEngine(test3.this);
    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
    locationEngine.activate();

    Location lastLocation = locationEngine.getLastLocation();
    if (lastLocation != null) {
        originLocation = lastLocation;
        setCameraPosition(lastLocation);
    } else {
        locationEngine.addLocationEngineListener(this);
    }
}

private void setCameraPosition(Location location) {
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(location.getLatitude(), location.getLongitude()), 13));
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {

}

@Override
public void onPermissionResult(boolean granted) {
    if (granted) {
        enableLocationPlugin();
    } else {
        finish();
    }
}

@Override
@SuppressWarnings( {"MissingPermission"})
public void onConnected() {
    locationEngine.requestLocationUpdates();
}

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        originLocation = location;
        setCameraPosition(location);
        locationEngine.removeLocationEngineListener(this);
    }
}

@Override
@SuppressWarnings( {"MissingPermission"})
protected void onStart() {
    super.onStart();
    if (locationEngine != null) {
        locationEngine.requestLocationUpdates();
    }
    if (locationPlugin != null) {
        locationPlugin.onStart();
    }
    mapView.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    if (locationEngine != null) {
        locationEngine.removeLocationUpdates();
    }
    if (locationPlugin != null) {
        locationPlugin.onStop();
    }
    mapView.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
    if (locationEngine != null) {
        locationEngine.deactivate();
    }
}

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

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

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

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

} ``

danesfeder commented 6 years ago

@steven-haddad would you be able to record a gif or screenshot of the behavior you're seeing? Also, what dependency are you using to compile the navigation UI?

steven-haddad commented 6 years ago

Also the navigation work fine when i pass the coordinates on parameters

The dependency: "compile('com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.6.1')"

The fist step when the Api calculate the optimized route: 1

The second step, when the navigation API display me white screen 2

Guardiola31337 commented 6 years ago

Hey 👋 @steven-haddad Thank you for the detailed info.

Current release version is 0.11.1. Just to confirm that the issue is still present in the latest version, would you mind retesting it using 0.11.1 version?

danesfeder commented 6 years ago

Hey @steven-haddad, feel free to test with our latest version and cut a new ticket if your issue persists. Thanks!