Я хочу найти текущее местоположение на подключенном. В настоящее время я использую FusedLocationApi, который устарел. Я должен использовать метод requestLocationUpdate, но я получаю некоторую синтаксическую ошибку при использовании FusedLocationProviderClient. я также должен использовать removeLocationupdate в onPause и onDestroy.
Это мой текущий код, который использует FusedLocationProviderClient-
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClientApi, mLocationRequest, this);
if (mCurrentLocMarker == null) {
Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleClientApi);
mLastLocation = loc;
if (loc != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Location");
markerOptions.flat(true);
int height = 150;
int width = 150;
BitmapDrawable bitmapDrawable = (BitmapDrawable)getResources().getDrawable(R.drawable.icon_navigation);
Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap marker = Bitmap.createScaledBitmap(bitmap, width, height, false);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(marker));
markerOptions.anchor(0.5f, 0.5f);
mCurrentLocMarker = mMap.addMarker(markerOptions);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.zoom(17)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
String trackid = SharedPreferenceManager.getmInstance(this).getTrackId();
if (mGoogleClientApi != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleClientApi,this);
}
if (!trackid.equals("NO DATA")) {
startService(new Intent(this, LocationService.class));
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
String trackid = SharedPreferenceManager.getmInstance(this).getTrackId();
if (mGoogleClientApi != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleClientApi, this);
}
if (!trackid.equals("NO DATA")) {
startService(new Intent(this, LocationService.class));
}
}
Это я пытался, но не могу реализовать свои requestLocationUpdate и removeLocationUpdate:
FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate