目标触发事件到达时Mapbox到达

时间:2019-06-16 02:48:43

标签: java android

我正在使用提供Mapbox导航的送货服务开发一个Android应用程序。除了覆盖onArrival方法之外,其他所有操作都进行得很顺利。用户到达目的地后,我想触发一个事件。

public class MapActivity extends AppCompatActivity implements NavigationMetricListeners, OnMapReadyCallback, LocationEngineListener, PermissionsListener, NavigationMetricListeners.ArrivalListener, MapboxMap.OnMapClickListener {

private MapView mapView;
private MapboxMap map;
private PermissionsManager permissionsManager;
private LocationEngine locationEngine;
private LocationLayerPlugin locationLayerPlugin;
private Location originLocation;
private Point originPosition, desitinationPosition;
private Marker destinantionMarker;
private Button startNav;
private NavigationMapRoute navigationMapRoute;
private static final String TAG = "MainActivity";
private Double lat, lon;
private LatLng latLng2;


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

    mapView =  findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    lat = Double.valueOf(getIntent().getStringExtra("latitude"));
    lon = Double.valueOf(getIntent().getStringExtra("longitude"));

    mapView.getMapAsync(this);


    Toast.makeText(getApplicationContext(),String.valueOf(lat),Toast.LENGTH_SHORT).show();


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

            NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                    .origin(originPosition)
                    .destination(desitinationPosition)
                    .shouldSimulateRoute(true)
                    .build();
            NavigationLauncher.startNavigation(MapActivity.this,options);

        }
    });



}

@Override
public void onMapReady(MapboxMap mapboxMap) {

    map =  mapboxMap;
    //map.addOnMapClickListener(this);
    enableLocation();


    Toast.makeText(getApplicationContext(),"Map is ready...",Toast.LENGTH_SHORT).show();
    latLng2 = new LatLng(lat, lon);

    setRoute(latLng2);


    //startNav.setEnabled(true);

}

private void enableLocation(){

    if(PermissionsManager.areLocationPermissionsGranted(this)){

        initializeLocationEngine();
        initializeLocationLayer();

    }
    else{
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}

@SuppressWarnings("MissingPermission")
private void initializeLocationEngine(){

    locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
    locationEngine.activate();

    Location lastLocation = locationEngine.getLastLocation();

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

@SuppressWarnings("MissingPermission")
private void initializeLocationLayer(){

    locationLayerPlugin =  new LocationLayerPlugin(mapView, map, locationEngine);
    locationLayerPlugin.setLocationLayerEnabled(true);
    locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
    locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
}

private void setCameraPosition(Location location){

    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),13.0));

}

private void setRoute(@NonNull LatLng latLng){

    if(destinantionMarker != null ){

        map.removeMarker(destinantionMarker);
    }

    destinantionMarker = map.addMarker(new MarkerOptions().position(latLng));

    desitinationPosition = Point.fromLngLat(latLng.getLongitude(),latLng.getLatitude());
    originPosition = Point.fromLngLat(lon,lat);
    getRoute(originPosition, desitinationPosition);

    //startNav.setEnabled(true);
    //startNav.setBackgroundResource(R.color.transparent);

}

@Override
public void onMapClick(@NonNull LatLng point) {

    if(destinantionMarker != null ){

        map.removeMarker(destinantionMarker);
    }

    destinantionMarker = map.addMarker(new MarkerOptions().position(point));

    desitinationPosition = Point.fromLngLat(point.getLongitude(),point.getLatitude());
    originPosition = Point.fromLngLat(originLocation.getLongitude(),originLocation.getLatitude());
    getRoute(originPosition, desitinationPosition);

    startNav.setEnabled(true);
    startNav.setBackgroundResource(R.color.transparent);

}

private void getRoute(Point origin, Point destination){

    NavigationRoute.builder().
            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){

                        Log.e(TAG, "No routes found..");
                    }
                    else if(response.body().routes().size() == 0){

                        Log.e(TAG, "No routes found");

                    }

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

                    if(navigationMapRoute != null){
                        navigationMapRoute.removeRoute();
                    }
                    else{
                        navigationMapRoute =  new NavigationMapRoute(null,mapView,map);
                    }
                    navigationMapRoute.addRoute(currentRoute);
                    startNav.setEnabled(true);


                }

                @Override
                public void onFailure(Call<DirectionsResponse> call, Throwable t) {

                    Toast.makeText(getApplicationContext(),"Error..",Toast.LENGTH_SHORT).show();

                }
            });

}

@Override
@SuppressWarnings("MissingPermission")
public void onConnected() {

    locationEngine.requestLocationUpdates();

}

@Override
public void onLocationChanged(Location location) {

    if(location !=null){
        originLocation = location;
        setCameraPosition(location);
    }

}

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

    Toast.makeText(this,"Location permission is required for the navigation process...!",Toast.LENGTH_LONG).show();
}

@Override
public void onPermissionResult(boolean granted) {

    if(granted){
        enableLocation();
    }
}

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

@Override
@SuppressWarnings("MissingPermission")
protected void onStart() {
    super.onStart();

    if(locationEngine != null){
        locationEngine.requestLocationUpdates();
    }
    if(locationLayerPlugin != null){
        locationLayerPlugin.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();

    if(locationEngine != null){

        locationEngine.removeLocationUpdates();
    }
    if(locationLayerPlugin != null){

        locationLayerPlugin.onStop();
    }
    mapView.onStop();

}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    mapView.onSaveInstanceState(outState);
}

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

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

    if(locationEngine != null){

        locationEngine.deactivate();
    }
    mapView.onDestroy();

}


@Override
public void onArrival(Location location, RouteProgress routeProgress) {

    Toast.makeText(getApplicationContext(),"You have arrived...!",Toast.LENGTH_SHORT).show();

}
}

0 个答案:

没有答案