删除android上的mapkit yandex路由

idv4meu8  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(221)

如何删除中的内置路由 Mapkit Yandex . 我想在用户移动时更新我的路线。我使用 LocationManager 每次创建一个新路由以使对象移动。但无法删除旧路由。
我的代码

public class MapKitFragment  extends Fragment implements DrivingSession.DrivingRouteListener{

    private final String MAPKIT_API_KEY = "This my key";
    private static final double DESIRED_ACCURACY = 0;
    private static final long MINIMAL_TIME = 1000;
    private static final double MINIMAL_DISTANCE = 1;
    private static final boolean USE_IN_BACKGROUND = false;
    public static final int COMFORTABLE_ZOOM_LEVEL = 18;
    private Point startLocation = MapKitService.getInstance().getRouteStartLocation();
    private final Point ROUTE_END_LOCATION = MapKitService.getInstance().getRouteEndLocation();
    private CustomMapView mapView;
    private MapObjectCollection mapObjects;
    private DrivingRouter drivingRouter;
    private DrivingSession drivingSession;
    public static final String TAG = "mapkit";
    private LocationManager locationManager;
    private LocationListener myLocationListener;
    DrivingOptions drivingOptions;
    VehicleOptions vehicleOptions;
    ArrayList<RequestPoint> requestPoints;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
         drivingOptions = new DrivingOptions();
         vehicleOptions = new VehicleOptions();
        requestPoints = new ArrayList<>();
        MapKitFactory.setApiKey(MAPKIT_API_KEY);
        MapKitFactory.initialize(this.getContext());
        DirectionsFactory.initialize(this.getContext());
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_mapkit, container, false);
        mapView = (CustomMapView) rootView.findViewById(R.id.mapview);
        drivingRouter = DirectionsFactory.getInstance().createDrivingRouter();
        mapObjects = mapView.getMap().getMapObjects().addCollection();
        initLocationManager();
        submitRequest();
        mapView.getMap().move(new CameraPosition(
                startLocation, 18, 0, 0));
        return rootView;

    }

    @Override
    public void onStop() {
        MapKitFactory.getInstance().onStop();
        super.onStop();
    }

    @Override
    public void onStart() {
        super.onStart();
        MapKitFactory.getInstance().onStart();
        mapView.onStart();
        subscribeToLocationUpdate();
    }

    @Override
    public void onDrivingRoutes(List<DrivingRoute> routes) {
        for (DrivingRoute route : routes) {
            mapObjects.addPolyline(route.getGeometry());
        }
    }

    @Override
    public void onDrivingRoutesError(@NonNull Error error) {
        String errorMessage = getString(R.string.unknown_error_message);
        if (error instanceof RemoteError) {
            errorMessage = getString(R.string.remote_error_message);
        } else if (error instanceof NetworkError) {
            errorMessage = getString(R.string.network_error_message);
        }

        Toast.makeText(this.getContext(), errorMessage, Toast.LENGTH_SHORT).show();
    }

    public void submitRequest() {
        requestPoints.add(new RequestPoint(
                startLocation,
                RequestPointType.WAYPOINT,
                null));
        requestPoints.add(new RequestPoint(
                ROUTE_END_LOCATION,
                RequestPointType.WAYPOINT,
                null));
        drivingSession = drivingRouter.requestRoutes(requestPoints, drivingOptions, vehicleOptions, this);
    }

    private void subscribeToLocationUpdate() {
        if (locationManager != null && myLocationListener != null) {
            locationManager.subscribeForLocationUpdates(DESIRED_ACCURACY, MINIMAL_TIME, MINIMAL_DISTANCE, USE_IN_BACKGROUND, FilteringMode.OFF, myLocationListener);
        }
    }

    private void moveCamera(Point point, float zoom) {
        mapView.getMap().move(
                new CameraPosition(point, zoom, 0.0f, 0.0f),
                new Animation(Animation.Type.SMOOTH, 1),
                null);
    }

    private void initLocationManager(){
        locationManager = MapKitFactory.getInstance().createLocationManager();
        myLocationListener = new LocationListener() {
            @Override
            public void onLocationUpdated(Location location) {
                if (startLocation == null) {
                    moveCamera(location.getPosition(), COMFORTABLE_ZOOM_LEVEL);
                }
                startLocation = location.getPosition();
                updateRequest(location.getPosition());
            }

            @Override
            public void onLocationStatusUpdated(LocationStatus locationStatus) {
                if (locationStatus == LocationStatus.NOT_AVAILABLE) {

                }
            }
        };
    }
    public void updateRequest(Point startLocation) {
        drivingSession.cancel();
        requestPoints.add(new RequestPoint(
                startLocation,
                RequestPointType.WAYPOINT,
                null));
        requestPoints.add(new RequestPoint(
                ROUTE_END_LOCATION,
                RequestPointType.WAYPOINT,
                null));
        drivingSession = drivingRouter.requestRoutes(requestPoints, drivingOptions, vehicleOptions, this);
        mapView.getMap().move(new CameraPosition(
                startLocation, 18, 0, 0));
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题