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

OnProgressChange and OnLocationChanged not working #781

Closed
alecmn opened this issue Mar 19, 2018 · 10 comments
Closed

OnProgressChange and OnLocationChanged not working #781

alecmn opened this issue Mar 19, 2018 · 10 comments

Comments

@alecmn
Copy link

alecmn commented Mar 19, 2018

I followed the android navogation app tutorial on the mapbox website and got it working. Now i want to implement an OnProgressChange method, but it is not being called. I read somewhere that it could be because speed is 0, so i made an OnLocationChanged method to print the speed to see what the values is. But the OnLocationChanged method is also not being called. I am using com.mapbox.mapboxsdk:mapbox-android-navigation:0.11.1, my minSdkVersion is 15 and my targetSdkVersion is 26.
Here is my code for both methods:

@Override public void onProgressChange (Location location, RouteProgress routeProgress){ Toast.makeText(getBaseContext(), "Progress changing ", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Distance remaining: " + stepDistanceRemaining, Toast.LENGTH_SHORT).show(); if (stepDistanceRemaining < 50) { Log.i(TAG, "NextStepComing"); sendManeuver(routeProgress); } }

@Override public void onLocationChanged(Location location) { String speed = String.format("%d\nMPH", (int) (location.getSpeed() * 2.2369)); Toast.makeText(getBaseContext(), "Speed: " + speed, Toast.LENGTH_SHORT).show(); if (location != null) { originLocation = location; setCameraPosition(location); } }

@danesfeder
Copy link
Contributor

Hey @A1ecm, thanks for the feedback here - are you using these listeners in conjunction with NavigationLauncher?

@alecmn
Copy link
Author

alecmn commented Mar 19, 2018

Yes, this is my NavigationLauncher code:

`

                public void onClick(View v) {
                    Point origin = originPosition;
                    Point destination = destinationPosition;

                    String awsPoolId = null;

                    boolean simulateRoute = false;

                    NavigationViewOptions options = NavigationViewOptions.builder()
                            .directionsProfile(DirectionsCriteria.PROFILE_CYCLING)
                            .origin(origin)
                            .destination(destination)
                            .awsPoolId(awsPoolId)
                            .shouldSimulateRoute(simulateRoute)
                            .build();

                    // Call this method with Context from within an Activity
                    NavigationLauncher.startNavigation(MainActivity.this, options);`

Thanks for the response

@danesfeder
Copy link
Contributor

@A1ecm Unfortunately, this is an existing bug with the NavigationLauncher implementation. Because we launch NavigationActivity via an Intent, we aren't able to handle the transfer of the listener references.

This is currently confusing because NavigationViewOptions implies that we are able to support this. #782 was just created to make separate options and avoid this confusion.

Using the NavigationView without NavigationLauncher allows you to add these listeners to your own Activity and would solve your issue here: https://www.mapbox.com/android-docs/navigation/overview/navigation-ui/#navigationview

@alecmn
Copy link
Author

alecmn commented Mar 19, 2018

Thanks for the solution, does this mean i have to make two seperate fragments or can i use the NavigationView in my MainActivity?

@danesfeder
Copy link
Contributor

@A1ecm You can use NavigationView like any other vanilla View in your Activity or Fragment

@alecmn
Copy link
Author

alecmn commented Mar 19, 2018

I implemented it like this:

`

@OverRide
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "pk.eyJ1IjoiYTFlYyIsImEiOiJjamVhMTJudXoxOGk0MnFudGV2Z3NnMGpwIn0.p9G0Ejx0EGALsmAOJt4ICQ");
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@OverRide
public void onMapReady(final MapboxMap mapboxMap) {
map = mapboxMap;
enableLocationPlugin();
originCoord = new LatLng(originLocation.getLatitude(), originLocation.getLongitude());

            mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
                @Override
                public void onMapClick(@NonNull LatLng point) {

                    if (destinationMarker != null) {
                        mapboxMap.removeMarker(destinationMarker);
                    }

                    destinationCoord = point;

                    destinationMarker = mapboxMap.addMarker(new MarkerOptions()
                            .position(destinationCoord)
                    );
                    destinationPosition = Point.fromLngLat(destinationCoord.getLongitude(), destinationCoord.getLatitude());
                    originPosition = Point.fromLngLat(originCoord.getLongitude(), originCoord.getLatitude());
                    getRoute(originPosition, destinationPosition);
                    button.setEnabled(true);
                    button.setBackgroundResource(R.color.mapboxBlue);

                };
            });
            button = findViewById(R.id.startButton);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    setContentView(R.layout.activity_navigation);
                    navigationView = findViewById(R.id.navigationView);
                    navigationView.onCreate(savedInstanceState);
                    navigationView.getNavigationAsync(new OnNavigationReadyCallback() {
                        @Override
                        public void onNavigationReady() {
                            Point origin = originPosition;
                            Point destination = destinationPosition;

                            String awsPoolId = null;

                            boolean simulateRoute = false;

                            MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder()
                                    .unitType(NavigationUnitType.TYPE_METRIC)
                                    .build();

                            NavigationViewOptions options = NavigationViewOptions.builder()
                                    .navigationOptions(navigationOptions)
                                    .directionsProfile(DirectionsCriteria.PROFILE_CYCLING)
                                    .progressChangeListener(this)
                                    .origin(origin)
                                    .destination(destination)
                                    .awsPoolId(awsPoolId)
                                    .shouldSimulateRoute(simulateRoute)
                                    .build();

                            navigationView.startNavigation(options);


                        }

                    });
                }
            });
        };
    });
}`

But the "this" in the builder cannot be applied. Any insights?

@danesfeder
Copy link
Contributor

danesfeder commented Mar 19, 2018

@A1ecm NameOfYourActivityOrFragment.this should fix that

@alecmn
Copy link
Author

alecmn commented Mar 19, 2018

It works, thank you so much! I have one last thing i'm going to bother you with. I can't seem to find the right place to place this code for ending the navigation.
@Override public void onNavigationFinished() { finish(); }
If I place it where the documentation says I should place it, it says that the method does not override it's superclass and is never used.

@jethromayuk
Copy link

Question @A1ecm, did you end up creating two separate activities? The main activity having the mapview and the navigation activity holding the navigationview?

@danesfeder
Copy link
Contributor

danesfeder commented Mar 20, 2018

@A1ecm you need to make sure YourActivity implements NavigationListener then pass it into your NavigationViewOptions#navigationListener(YourActivity.this). If you have any other issues, feel free to open a new ticket. Closing this now, as the OP has been resolved.

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