NavigationView with useroffroute MapBox Android

huangapple go评论84阅读模式
英文:

NavigationView with useroffroute MapBox Android

问题

public class mapbox extends AppCompatActivity {

    // ... (other imports and variable declarations)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
        setContentView(R.layout.activity_mapbox);

        button = findViewById(R.id.button);

        MapboxNavigationOptions options = MapboxNavigationOptions.builder().isDebugLoggingEnabled(true).build();
        navigation = new MapboxNavigation(getApplicationContext(), getString(R.string.mapbox_access_token), options);

        navigation.addOffRouteListener(new OffRouteListener() {
            @Override
            public void userOffRoute(Location location) {
                Toast.makeText(getApplicationContext(), "Off route detected.........", Toast.LENGTH_SHORT).show();
                // Make sure you call for a new DirectionsRoute object
                // and end by calling MapboxNavigation#startNavigation on a successful
            }
        });

        // ... (other code)

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // From Mapbox to The White House
                Point origin = Point.fromLngLat(-38.62882018, -3.78666528);
                Point destination = Point.fromLngLat(-38.56038094, -3.7337361F);

                NavigationRoute.builder(mapbox.this)
                        .accessToken(getString(R.string.mapbox_access_token))
                        .origin(origin)
                        .destination(destination)
                        .build()
                        .getRoute(new Callback<DirectionsResponse>() {
                            @Override
                            public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                                Log.i(TAG, response+"");

                                // Route fetched from NavigationRoute
                                DirectionsRoute route = response.body().routes().get(0);

                                // Create a NavigationLauncherOptions object to package everything together
                                NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                                        .directionsRoute(route)
                                        .shouldSimulateRoute(false)
                                        .build();

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

                            @Override
                            public void onFailure(Call<DirectionsResponse> call, Throwable t) {
                                // Handle failure
                            }
                        });
            }
        });

        // ... (other lifecycle methods)
    }

    // ... (other methods and lifecycle overrides)
}
英文:

I'm actually trying to use the useroffroute function but it's not working, I saw this other post and it said to NavigationView, only I don't know how to do it exactly. I am currently using this code to detect if the user has left the route, but he is not calling the useroffroute function. What I'm trying to do is that when the user leaves the route he fires a Toast for the user, but unfortunately I was not successful.

public class mapbox extends AppCompatActivity{
private MapView mapView;
Button button;
private static final String TAG = &quot;resultados&quot;;
private MapboxNavigation navigation;
private boolean running;
private NavigationMapboxMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
setContentView(R.layout.activity_mapbox);
button = findViewById(R.id.button);
MapboxNavigationOptions options = MapboxNavigationOptions.builder().isDebugLoggingEnabled(true).build();
navigation = new MapboxNavigation(getApplicationContext(), getString(R.string.mapbox_access_token), options);
/*
navigation.addOffRouteListener(new OffRouteListener() {
@Override
public void userOffRoute(Location location) {
Toast.makeText(getApplicationContext(), &quot;Off route detected.........&quot;,        Toast.LENGTH_SHORT).show();
// Make sure you call for a new DirectionsRoute object
// and end by calling MapboxNavigation#startNavigation on a successful
}
});
*/
/*
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// From Mapbox to The White House
Point origin = Point.fromLngLat(-38.62882018, -3.78666528);
Point destination = Point.fromLngLat(-38.56038094, -3.7337361F);
NavigationRoute.builder(mapbox.this)
.accessToken(getString(R.string.mapbox_access_token))
.origin(origin)
.destination(destination)
.build()
.getRoute(new Callback&lt;DirectionsResponse&gt;() {
@Override
public void onResponse(Call&lt;DirectionsResponse&gt; call, Response&lt;DirectionsResponse&gt; response) {
Log.i(TAG, response+&quot;&quot;);
// Route fetched from NavigationRoute
DirectionsRoute route = response.body().routes().get(0);
// Create a NavigationLauncherOptions object to package everything together
NavigationLauncherOptions options =   NavigationLauncherOptions.builder()
.directionsRoute(route)
.shouldSimulateRoute(false)
.build();
// Call this method with Context from within an Activity
NavigationLauncher.startNavigation(mapbox.this, options);
}
@Override
public void onFailure(Call&lt;DirectionsResponse&gt; call, Throwable t) {
}
});
}
});
}
@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
public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
//mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
//mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
//mapView.onDestroy();
}
}

答案1

得分: 1

以下是翻译好的部分:

public class last extends AppCompatActivity implements OnNavigationReadyCallback,
    NavigationListener, RouteListener, ProgressChangeListener {

    private NavigationView navigationView;
    private boolean dropoffDialogShown;
    private Location lastKnownLocation;

    private List<Point> points = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
        setTheme(R.style.Theme_AppCompat_NoActionBar);
        super.onCreate(savedInstanceState);
        points.add(Point.fromLngLat(-38.62882018, -3.78666528));
        points.add(Point.fromLngLat(-38.56038094, -3.7337361));
        setContentView(R.layout.activity_last);
        navigationView = findViewById(R.id.navigationView);
        navigationView.onCreate(savedInstanceState);
        navigationView.initialize(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        navigationView.onStart();
    }

    // 更多的生命周期回调...

    @Override
    public void onNavigationReady(boolean isRunning) {
        fetchRoute(points.remove(0), points.remove(0));
    }

    // 更多的回调方法...

    @Override
    public void onArrival() {
        if (!dropoffDialogShown && !points.isEmpty()) {
            showDropoffDialog();
            dropoffDialogShown = true;
            Toast.makeText(this, "You have arrived!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onProgressChange(Location location, RouteProgress routeProgress) {
        lastKnownLocation = location;
    }

    private void startNavigation(DirectionsRoute directionsRoute) {
        NavigationViewOptions navigationViewOptions = setupOptions(directionsRoute);
        navigationView.startNavigation(navigationViewOptions);
    }

    private void showDropoffDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setMessage(getString(R.string.dropoff_dialog_text));
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dropoff_dialog_positive_text),
            (dialogInterface, in) -> fetchRoute(getLastKnownLocation(), points.remove(0)));
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dropoff_dialog_negative_text),
            (dialogInterface, in) -> {
                // Do nothing
            });

        alertDialog.show();
    }

    private void fetchRoute(Point origin, Point destination) {
        NavigationRoute.builder(this)
            .accessToken(Mapbox.getAccessToken())
            .origin(origin)
            .destination(destination)
            .alternatives(true)
            .build()
            .getRoute(new Callback<DirectionsResponse>() {
                @Override
                public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
                    DirectionsResponse directionsResponse = response.body();
                    if (directionsResponse != null && !directionsResponse.routes().isEmpty()) {
                        startNavigation(directionsResponse.routes().get(0));
                    }
                }

                @Override
                public void onFailure(Call<DirectionsResponse> call, Throwable t) {
                    // Handle failure
                }
            });
    }

    // 更多的方法...
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mapbox.services.android.navigation.ui.v5.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:navigationLightTheme="@style/NavigationViewLight"
        app:navigationDarkTheme="@style/NavigationViewDark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
英文:

After many hours looking for a solution, I finally found one. You have to use NavigationViewer to obtain the parameters that the application passes, so you can listen if the user leaves the route. Here's an example:

public class last extends AppCompatActivity implements   OnNavigationReadyCallback,
NavigationListener, RouteListener, ProgressChangeListener {
private NavigationView navigationView;
private boolean dropoffDialogShown;
private Location lastKnownLocation;
private List&lt;Point&gt; points = new ArrayList&lt;&gt;();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
setTheme(R.style.Theme_AppCompat_NoActionBar);
super.onCreate(savedInstanceState);
points.add(Point.fromLngLat(-38.62882018, -3.78666528));
points.add(Point.fromLngLat(-38.56038094, -3.7337361));
setContentView(R.layout.activity_last);
navigationView = findViewById(R.id.navigationView);
navigationView.onCreate(savedInstanceState);
navigationView.initialize(this);
}
@Override
public void onStart() {
super.onStart();
navigationView.onStart();
}
@Override
public void onResume() {
super.onResume();
navigationView.onResume();
}
@Override
public void onLowMemory() {
super.onLowMemory();
navigationView.onLowMemory();
}
@Override
public void onBackPressed() {
// If the navigation view didn&#39;t need to do anything, call super
if (!navigationView.onBackPressed()) {
super.onBackPressed();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
navigationView.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
navigationView.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
navigationView.onPause();
}
@Override
public void onStop() {
super.onStop();
navigationView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
navigationView.onDestroy();
}
@Override
public void onNavigationReady(boolean isRunning) {
fetchRoute(points.remove(0), points.remove(0));
}
@Override
public void onCancelNavigation() {
// Navigation canceled, finish the activity
finish();
}
@Override
public void onNavigationFinished() {
// Intentionally empty
}
@Override
public void onNavigationRunning() {
// Intentionally empty
}
@Override
public boolean allowRerouteFrom(Point offRoutePoint) {
return true;
}
@Override
public void onOffRoute(Point offRoutePoint) {
Toast.makeText(this, &quot;Off route&quot;, Toast.LENGTH_SHORT).show();
}
@Override
public void onRerouteAlong(DirectionsRoute directionsRoute) {
}
@Override
public void onFailedReroute(String errorMessage) {
}
@Override
public void onArrival() {
if (!dropoffDialogShown &amp;&amp; !points.isEmpty()) {
showDropoffDialog();
dropoffDialogShown = true; // Accounts for multiple arrival events
Toast.makeText(this, &quot;You have arrived!&quot;, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
lastKnownLocation = location;
}
private void startNavigation(DirectionsRoute directionsRoute) {
NavigationViewOptions navigationViewOptions = setupOptions(directionsRoute);
navigationView.startNavigation(navigationViewOptions);
}
private void showDropoffDialog() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage(getString(R.string.dropoff_dialog_text));
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dropoff_dialog_positive_text),
(dialogInterface, in) -&gt; fetchRoute(getLastKnownLocation(), points.remove(0)));
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dropoff_dialog_negative_text),
(dialogInterface, in) -&gt; {
// Do nothing
});
alertDialog.show();
}
private void fetchRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination)
.alternatives(true)
.build()
.getRoute(new Callback&lt;DirectionsResponse&gt;() {
@Override
public void onResponse(Call&lt;DirectionsResponse&gt; call, Response&lt;DirectionsResponse&gt; response) {
DirectionsResponse directionsResponse = response.body();
if (directionsResponse != null &amp;&amp; !directionsResponse.routes().isEmpty()) {
startNavigation(directionsResponse.routes().get(0));
}
}
@Override
public void onFailure(Call&lt;DirectionsResponse&gt; call, Throwable t) {
}
});
}
private NavigationViewOptions setupOptions(DirectionsRoute directionsRoute) {
dropoffDialogShown = false;
NavigationViewOptions.Builder options = NavigationViewOptions.builder();
options.directionsRoute(directionsRoute)
.navigationListener(this)
.progressChangeListener(this)
.routeListener(this)
.shouldSimulateRoute(false);
return options.build();
}
private Point getLastKnownLocation() {
return Point.fromLngLat(lastKnownLocation.getLongitude(), lastKnownLocation.getLatitude());
}

}

XML File:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;com.mapbox.services.android.navigation.ui.v5.NavigationView
android:id=&quot;@+id/navigationView&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:navigationLightTheme=&quot;@style/NavigationViewLight&quot;
app:navigationDarkTheme=&quot;@style/NavigationViewDark&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案2

得分: 0

当我开始使用多个途径点进行导航时导航在达到第一个途径点后停止
请提供建议更改

try {
    NavigationRoute.Builder builder = NavigationRoute.builder(this)
        .accessToken("pk." + getString(R.string.gh_key))
        .baseUrl(getString(R.string.base_url))
        .user("gh")
        .alternatives(true);

    Location lastKnownLocation = new Location(LocationManager.GPS_PROVIDER);
    lastKnownLocation.setLatitude(Double.parseDouble(SharePref.getInstance(MapBoxNavActivity.this).getJavaRXPref(getString(R.string.latitude))));
    lastKnownLocation.setLongitude(Double.parseDouble(SharePref.getInstance(MapBoxNavActivity.this).getJavaRXPref(getString(R.string.longitude))));
    Point location = Point.fromLngLat(lastKnownLocation.getLongitude(), lastKnownLocation.getLatitude());
    if (lastKnownLocation.hasBearing()) {
        // 90 seems to be the default tolerance of the SDK
        builder.origin(location, (double) lastKnownLocation.getBearing(), 90.0);
    } else {
        builder.origin(location);
    }

    try {
        if (waypoints.size() > 0) {
            for (int i = 0; i < waypoints.size(); i++) {
                Point p = waypoints.get(i);
                if (i < waypoints.size() - 1) {
                    try {
                        builder.addWaypoint(p);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    builder.destination(p);
                }
            }
        }
    } catch (Exception se) {
        se.printStackTrace();
    }

    builder.build().getRoute(new SimplifiedCallback() {
        @Override
        public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
            if (validRouteResponse(response)) {
                route = response.body().routes().get(0);
                try {
                    MapboxNavigationOptions.Builder navigationOptions = MapboxNavigationOptions.builder();
                    NavigationViewOptions.Builder options = NavigationViewOptions.builder();
                    options.navigationListener(MapBoxNavActivity.this);
                    options.directionsRoute(route);
                    options.shouldSimulateRoute(false);
                    options.directionsProfile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC);
                    navigationOptions.enableOffRouteDetection(true);
                    navigationOptions.snapToRoute(true);
                    options.navigationOptions(navigationOptions.build());
                    navigationView.startNavigation(options.build());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
            super.onFailure(call, throwable);
            Log.i("Fetch route error", throwable.getMessage());
        }
    });
} catch (Exception se) {
    se.printStackTrace();
}
英文:

When I start navigation with multiple waypoints, the navigation stops after reaching first waypoint.
Please suggest changes.`

       try{
NavigationRoute.Builder builder = NavigationRoute.builder(this)
.accessToken(&quot;pk.&quot; + getString(R.string.gh_key))
.baseUrl(getString(R.string.base_url))
.user(&quot;gh&quot;)
.alternatives(true);
Location lastKnownLocation = new Location(LocationManager.GPS_PROVIDER);
lastKnownLocation.setLatitude(Double.parseDouble(SharePref.getInstance(MapBoxNavActivity.this).getJavaRXPref(getString(R.string.latitude))));
lastKnownLocation.setLongitude(Double.parseDouble(SharePref.getInstance(MapBoxNavActivity.this).getJavaRXPref(getString(R.string.longitude))));
Point location = Point.fromLngLat(lastKnownLocation.getLongitude(), lastKnownLocation.getLatitude());
if (lastKnownLocation.hasBearing()){
// 90 seems to be the default tolerance of the SDK
builder.origin(location, (double) lastKnownLocation.getBearing(), 90.0);
}
else{
builder.origin(location);
}
try {
if(waypoints.size()&gt;0){
for (int i = 0; i &lt; waypoints.size(); i++) {
Point p = waypoints.get(i);
if (i &lt; waypoints.size() - 1) {
try {
builder.addWaypoint(p);
}catch (Exception e){
e.printStackTrace();
}
} else {
builder.destination(p);
}
}
}
}catch (Exception se){
se.printStackTrace();
}
builder.build().getRoute(new SimplifiedCallback() {
@Override
public void onResponse(Call&lt;DirectionsResponse&gt; call, Response&lt;DirectionsResponse&gt; response) {
if (validRouteResponse(response)) {
route = response.body().routes().get(0);
try {
MapboxNavigationOptions.Builder navigationOptions = MapboxNavigationOptions.builder();
NavigationViewOptions.Builder options = NavigationViewOptions.builder();
options.navigationListener(MapBoxNavActivity.this);
options.directionsRoute(route);
options.shouldSimulateRoute(false);
options.directionsProfile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC);
navigationOptions.enableOffRouteDetection(true);
navigationOptions.snapToRoute(true);
options.navigationOptions(navigationOptions.build());
navigationView.startNavigation(options.build());
}catch (Exception e){
e.printStackTrace();
}
} 
}
@Override
public void onFailure(Call&lt;DirectionsResponse&gt; call, Throwable throwable) {
super.onFailure(call, throwable);
Log.i(&quot;Fetch route error&quot;,throwable.getMessage());
}
});
}
}
catch ( Exception se){
se.printStackTrace();
}
}

`

huangapple
  • 本文由 发表于 2020年4月10日 21:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定