因为我在我的应用程序中使用MAPBOX.我不知道如何获取当前lat和long.我用这种方式获取当前位置.但它显示空对象引用.请帮我解决我的问题
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);
}
});
解决方法:
mapboxMap.setMyLocationEnabled(真);已被弃用.
您需要使用locationEngine来获取lat和long
locationEngine = new LostLocationEngine(MainActivity.this);
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setInterval(5000);
locationEngine.activate();
Location lastLocation = locationEngine.getLastLocation();
使用lastLocation.getLatitude(),lastLocation.getLongitude()