Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nnavigation not work why, there is no error #26

Closed
MahmoudSabbah opened this issue May 1, 2017 · 9 comments
Closed

Nnavigation not work why, there is no error #26

MahmoudSabbah opened this issue May 1, 2017 · 9 comments

Comments

@MahmoudSabbah
Copy link

when I click start route, no thing happen the navigation not work why, there is no error

@cammace
Copy link
Contributor

cammace commented May 1, 2017

Could you provide more information? Sounds like it might be an issue with your location engine not being set properly. The navigation starts, it just doesn't receive any location updates.

@MahmoudSabbah
Copy link
Author

MahmoudSabbah commented May 1, 2017 via email

@MahmoudSabbah
Copy link
Author

MahmoudSabbah commented May 1, 2017 via email

@cammace
Copy link
Contributor

cammace commented May 1, 2017

I think you forgot to post your code @MahmoudSabbah, not seeing it on Github.

@MahmoudSabbah
Copy link
Author

MahmoudSabbah commented May 2, 2017 via email

@MahmoudSabbah
Copy link
Author

Please what exactly happen when I "startNavigation(route);"

@cammace
Copy link
Contributor

cammace commented May 3, 2017

I'm not sure what not working means, there could be several issues you are running into or it might be that you are not actually navigating along the route (it doesn't simulate the route but rather expects you to actually traverse along it). Without any additional information such as where your codes coming from I'm unable to help troubleshoot the issue. Going ahead and closing this issue but please feel free to leave a comment below if you have any additional information to provide.

@cammace cammace closed this as completed May 3, 2017
@MahmoudSabbah
Copy link
Author

MahmoudSabbah commented May 3, 2017

I send my code I implement all things

import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.LocationSource;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.Constants;
import com.mapbox.services.android.location.LostLocationEngine;
import com.mapbox.services.android.navigation.v5.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.NavigationConstants;
import com.mapbox.services.android.navigation.v5.RouteProgress;
import com.mapbox.services.android.navigation.v5.listeners.AlertLevelChangeListener;
import com.mapbox.services.android.navigation.v5.listeners.NavigationEventListener;
import com.mapbox.services.android.navigation.v5.listeners.OffRouteListener;
import com.mapbox.services.android.navigation.v5.listeners.ProgressChangeListener;
import com.mapbox.services.android.telemetry.location.LocationEngine;
import com.mapbox.services.android.telemetry.location.LocationEnginePriority;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
import com.mapbox.services.commons.geojson.LineString;
import com.mapbox.services.commons.models.Position;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

public class NavigationActivity extends AppCompatActivity implements OnMapReadyCallback, MapboxMap.OnMapClickListener,
        ProgressChangeListener, NavigationEventListener, AlertLevelChangeListener, OffRouteListener {

    // Map variables
    private MapView mapView;
    private Polyline routeLine;
    private MapboxMap mapboxMap;
    private Marker destinationMarker;

    // Navigation related variables
    private LocationEngine locationEngine;
    private MapboxNavigation navigation;
    private Button startRouteButton;
    private DirectionsRoute route;
    private Position destination;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_activity);

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


        navigation = new MapboxNavigation(this, Mapbox.getAccessToken());
        locationEngine = LocationSource.getLocationEngine(this);

        startRouteButton = (Button) findViewById(R.id.startRouteButton);
        startRouteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (navigation != null && route != null) {

                    // Hide the start button
                     startRouteButton.setVisibility(View.INVISIBLE);

                    // Attach all of our navigation listeners.
                    navigation.addNavigationEventListener(NavigationActivity.this);
                    navigation.addProgressChangeListener(NavigationActivity.this);
                    navigation.addAlertLevelChangeListener(NavigationActivity.this);

                    // Adjust location engine to force a gps reading every second. This isn't required but gives an overall
                    // better navigation experience for users. The updating only occurs if the user moves 3 meters or further
                    // from the last update.
                    locationEngine.setInterval(0);
                    locationEngine.setSmallestDisplacement(3.0f);
                    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
                    locationEngine.setFastestInterval(1000);


                    navigation.setLocationEngine(locationEngine);
                    navigation.startNavigation(route);
                }
            }
        });
    }

    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        this.mapboxMap = mapboxMap;
        mapboxMap.setOnMapClickListener(this);
        Snackbar.make(mapView, "Tap map to place destination", BaseTransientBottomBar.LENGTH_LONG).show();

        mapboxMap.moveCamera(CameraUpdateFactory.zoomBy(12));

        if (PermissionsManager.areLocationPermissionsGranted(this)) {
            mapboxMap.setMyLocationEnabled(true);
            mapboxMap.getTrackingSettings().setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
        }
    }

    @Override
    public void onMapClick(@NonNull LatLng point) {
        if (destinationMarker != null) {
            mapboxMap.removeMarker(destinationMarker);
        }
        destinationMarker = mapboxMap.addMarker(new MarkerOptions().position(point));

        startRouteButton.setVisibility(View.VISIBLE);

        this.destination = Position.fromCoordinates(point.getLongitude(), point.getLatitude());
        calculateRoute();
    }

    private void drawRouteLine(DirectionsRoute route) {
        List<Position> positions = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6).getCoordinates();
        List<LatLng> latLngs = new ArrayList<>();
        for (Position position : positions) {
            latLngs.add(new LatLng(position.getLatitude(), position.getLongitude()));
        }

        // Remove old route if currently being shown on map.
        if (routeLine != null) {
            mapboxMap.removePolyline(routeLine);
        }

        routeLine = mapboxMap.addPolyline(new PolylineOptions()
                .addAll(latLngs)
                .color(Color.parseColor("#56b881"))
                .width(5f));
    }

    private void calculateRoute() {
        Location userLocation = mapboxMap.getMyLocation();
        if (userLocation == null) {
            Timber.d("calculateRoute: User location is null, therefore, origin can't be set.");
            return;
        }

        Position origin = (Position.fromCoordinates(userLocation.getLongitude(), userLocation.getLatitude()));

        navigation.getRoute(origin, destination, new Callback<DirectionsResponse>() {
            @Override
            public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                DirectionsRoute route = response.body().getRoutes().get(0);
                NavigationActivity.this.route = route;
                drawRouteLine(route);
            }

            @Override
            public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
                Timber.e("onFailure: navigation.getRoute()", throwable);
            }
        });
    }

  /*
   * Navigation listeners
   */

    @Override
    public void onRunning(boolean running) {
        Log.e("onRunning",""+running);
        if (running) {
            Timber.d("onRunning: Started");
        } else {
            Timber.d("onRunning: Stopped");
        }
    }

    @Override
    public void onProgressChange(Location location, RouteProgress routeProgress) {
        Log.e("onProgressChange",""+location);
        Timber.d("onProgressChange: fraction of route traveled: %f", routeProgress.getFractionTraveled());
    }

    @Override
    public void onAlertLevelChange(int alertLevel, RouteProgress routeProgress) {

        switch (alertLevel) {
            case NavigationConstants.HIGH_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "HIGH", Toast.LENGTH_LONG).show();
                break;
            case NavigationConstants.MEDIUM_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "MEDIUM", Toast.LENGTH_LONG).show();
                break;
            case NavigationConstants.LOW_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "LOW", Toast.LENGTH_LONG).show();
                break;
            case NavigationConstants.ARRIVE_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "ARRIVE", Toast.LENGTH_LONG).show();
                break;
            case NavigationConstants.DEPART_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "DEPART", Toast.LENGTH_LONG).show();
                break;
            default:
            case NavigationConstants.NONE_ALERT_LEVEL:
                Toast.makeText(NavigationActivity.this, "NONE", Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void userOffRoute(Location location) {
        Position newOrigin = Position.fromCoordinates(location.getLongitude(), location.getLatitude());
        navigation.updateRoute(newOrigin, destination, new Callback<DirectionsResponse>() {
            @Override
            public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                DirectionsRoute route = response.body().getRoutes().get(0);
                NavigationActivity.this.route = route;

                // Remove old route line from map and draw the new one.
                mapboxMap.removePolyline(routeLine);
                drawRouteLine(route);
            }

            @Override
            public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
                Timber.e("onFailure: navigation.getRoute()", throwable);
            }
        });
    }

  /*
   * Activity lifecycle methods
   */

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

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

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

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

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

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

        // Remove all navigation listeners
        navigation.removeAlertLevelChangeListener(this);
        navigation.removeNavigationEventListener(this);
        navigation.removeProgressChangeListener(this);
        navigation.removeOffRouteListener(this);

        // End the navigation session
        navigation.endNavigation();
    }

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

@mecoFarid
Copy link

@MahmoudSabbah Did it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants