我正在尝试使用融合位置API获取当前位置。
private void setInitialLocation() {
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, android.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(googleApiClient, request, new LocationListener(){
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
try {
} catch (Exception ex) {
ex.printStackTrace();
Log.e("MapException", ex.getMessage());
}
}
});
}
android studio发出此错误
类'从LocationListener派生的Annonymous类'必须是 声明抽象或实现抽象方法。
当我实现位置监听器的所有抽象方法时。
答案 0 :(得分:1)
FusedLocationApi
已弃用。使用此FusedLocationProviderClient
详细链接为here
创建FusedLocationProviderClient
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations, this can be null.
if (location != null) {
// Logic to handle location object
}
}
});