Android 如何用FusedLocationProviderClient替换FusedLocationApi?
我想在已连接的网络上查找当前位置。我目前使用的FusedLocationApi已被弃用。我必须使用requestLocationUpdate方法,但在使用FusedLocationProviderClient时出现一些语法错误。我还必须在onPause和onDestroy中使用removeLocationupdate 这是我当前使用FusedLocationProviderClient的代码-Android 如何用FusedLocationProviderClient替换FusedLocationApi?,android,google-maps,location,fusedlocationproviderapi,Android,Google Maps,Location,Fusedlocationproviderapi,我想在已连接的网络上查找当前位置。我目前使用的FusedLocationApi已被弃用。我必须使用requestLocationUpdate方法,但在使用FusedLocationProviderClient时出现一些语法错误。我还必须在onPause和onDestroy中使用removeLocationupdate 这是我当前使用FusedLocationProviderClient的代码- @Override public void onConnected(@Nullable Bundle
@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
你的开始就像你需要的那样
FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate
要从另一个应用程序以谷歌地图的形式获取位置,您可以使用mFusedLocationClient.getLastLocation()
它有监听器,addOnSuccessListener
提供给您位置,addOnCompleteListener
和task.getResult()==null
表示您需要执行自己的请求,如mFusedLocationClient.RequestLocationUpdate(getLocationRequest(),mlLocationCallback,null)代码>
例如,您需要在onPause中删除侦听器,如mFusedLocationClient.removeLocationUpdates(mLocationCallback)
你可以看看这个例子
而且,你的开始就像你需要的那样
FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate
要从另一个应用程序以谷歌地图的形式获取位置,您可以使用mFusedLocationClient.getLastLocation()
它有监听器,addOnSuccessListener
提供给您位置,addOnCompleteListener
和task.getResult()==null
表示您需要执行自己的请求,如mFusedLocationClient.RequestLocationUpdate(getLocationRequest(),mlLocationCallback,null)代码>
例如,您需要在onPause中删除侦听器,如mFusedLocationClient.removeLocationUpdates(mLocationCallback)
你可以看看这个例子
而且是的,我是这样理解的,但我对mlocationcallback的对象位置callback有点困惑,它来自com.google.android.gms.location.LocationCallback您需要将其设置为newlocationcallback()
,这样您就可以用locationResult覆盖方法onLocationResult
。如果您的locationResult==null
位置服务将关闭,您需要解决它。在另一种情况下,您需要by locationResult.getLocations(),您将获得location.getLatitude()和location.getLatitude(),是的,我得到了它,但我对mlocationCallback的对象LocationCallback有点困惑,它来自com.google.android.gms.location.LocationCallback您需要将其设置为newlocationcallback()
,这样您就可以用locationResult覆盖方法onLocationResult
。如果您的locationResult==null
位置服务将关闭,您需要解决它。在另一种情况下,您需要按locationResult.getLocations()进行排序,然后将得到location.getLatitude()和location.getLongitude()