以下代码是我的Android应用程序摘录以获取当前位置 我一直关注http://blog.teamtreehouse.com/beginners-guide-location-android和http://droidmentor.com/get-the-current-location-in-android/ 大多数文档甚至已经过时了developer.google和developers.android的内容。 我想知道为什么两个函数都返回null,我该怎么办呢 谢谢。
@Override
public void onConnected(Bundle bundle) {
try
{
Log.d(TAG, "connection successful");
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation == null) {
Log.d(TAG, "get last location = null");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); //then nothing happens
}
else {
Log.d(TAG, "last location successful");
handleNewLocation(mLastLocation);
}; }
@Override
public void onLocationChanged(Location location){
handleNewLocation(location);
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Address address = getAddress(location);
starttext.setText("Current Location: \n" + address.toString());
}
答案 0 :(得分:0)
FusedLocationProviderApi已弃用https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi 因此,请改用FusedLocationProviderClient。用法:https://developer.android.com/training/location/retrieve-current.html#play-services