我有以下簡單的類,這是我的主要活動。我基本上遵循了Android文檔中的逐字。我通過Android Studio在手機上運行此操作。我遇到的問題是我絕對從來沒有得到這個設置的合法位置。 location
對象始終爲空。實際上,我希望地址立即以的方式與瀏覽器通過navigator.geolocation
快速獲取位置的方式類似。從FusedLocationProviderApi沒有位置
有沒有什麼奇怪的方式,我有這樣的設置?如果不是,我該如何強制一個位置離開Fused Location Provider API?
public class MyActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Location location;
private GoogleApiClient mGoogleApiClient;
private static LocationRequest locationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
private void requestLocationUpdates() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(500);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationUpdates();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
location = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
}
你解決了你的問題嗎?有同樣的問題 – AAverin