如何在Android中的MAPBOX中获取当前位置

kal*_*ani 6 android mapbox mapbox-android

当我在应用程序中使用MAPBOX时,我不知道如何获取当前经纬度和经度。我使用这种方式获取当前位置,但其显示空对象引用,请帮助我解决问题

     mapView = (MapView)view. findViewById(R.id.mapView);
    mapView.setStyleUrl(Style.MAPBOX_STREETS);
    mapView.onCreate(savedInstanceState);

    //Add a mapBoxMap
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {
            mapboxMap.setMyLocationEnabled(true);
            // Set the origin waypoint to the devices location
            Position origin = Position.fromCoordinates(mapboxMap.getMyLocation().getLongitude(), mapboxMap.getMyLocation().getLatitude());

            Log.i("RR","mapboxMap.getMyLocation();"+origin);
           // Log.i("RR","mapboxMap.getMyLocation();"+mapboxMap.getMyLocation().getLongitude());
            mapboxMap.getUiSettings().setZoomControlsEnabled(true);
            mapboxMap.getUiSettings().setZoomGesturesEnabled(true);
            mapboxMap.getUiSettings().setScrollGesturesEnabled(true);
            mapboxMap.getUiSettings().setAllGesturesEnabled(true);


        }

    });
Run Code Online (Sandbox Code Playgroud)

小智 5

mapboxMap.setMyLocationEnabled(true);已经deprecated

你需要locationEngine像这样获取纬度和经度

    locationEngine = new LostLocationEngine(MainActivity.this);
    locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
    locationEngine.setInterval(5000);
    locationEngine.activate();
    Location lastLocation = locationEngine.getLastLocation();
Run Code Online (Sandbox Code Playgroud)

使用lastLocation.getLatitude()lastLocation.getLongitude()