无数个小时,我一直试图在启动我的应用时获取用户的当前位置,但之后我尝试的每个方法都返回了null。
我对GoogleApiClient的实例化发生在" onCreate"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
FirebaseMessagingHelper.registerDevice(this, FirebaseInstanceId.getInstance().getToken());
activity = this;
//xaxaxa
driverMapView = (MapView) findViewById(R.id.googleMapObject);
driverMapView.onCreate(savedInstanceState);
driverMapView.getMapAsync(this);
getUserToEnableCameraUsage();
if (googleApiClient == null)
{
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
googleApiClient.connect();

这是我项目中的代码(非常类似于api教程中提供的代码:
@Override
public void onConnected(@Nullable Bundle bundle)
{
int LOCATION_ALLOWED = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
if (LOCATION_ALLOWED != PackageManager.PERMISSION_GRANTED)
{
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastLocation != null)
{
driverGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()), 16));
testHelper.setDriverLatLngLocation(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()));
}
}
}
protected void createLocationRequest()
{
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(500);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
PendingResult<LocationSettingsResult> pendingResult = LocationServices
.SettingsApi
.checkLocationSettings(googleApiClient, locationSettingsRequestBuilder.build());
pendingResult.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult)
{
Status result = locationSettingsResult.getStatus();
if (result.getStatusCode() == LocationSettingsStatusCodes.SUCCESS)
{
requestingLocationUpdates = true;
startLocationUpdates();
Toast.makeText(MainActivity.this, "Gucci", Toast.LENGTH_SHORT).show();
}
if (result.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED)
{
requestingLocationUpdates = false;
Toast.makeText(MainActivity.this, "Please enable location services", Toast.LENGTH_SHORT).show();
}
if (result.getStatusCode() == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE)
{
requestingLocationUpdates = false;
Toast.makeText(MainActivity.this, "App cannot access settings", Toast.LENGTH_SHORT).show();
}
}
});
}
protected void startLocationUpdates()
{
int LOCATION_ALLOWED = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
if (LOCATION_ALLOWED != PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onLocationChanged(Location location)
{
driverGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16));
testHelper.setDriverLatLngLocation(new LatLng(location.getLatitude(), location.getLongitude()));
Toast.makeText(MainActivity.this, "It's working", Toast.LENGTH_SHORT).show();
}
protected void stopLocationUpdates()
{
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient, this);
requestingLocationUpdates = false;
}
&#13;
我在这里实例化&#34; createLocationRequest:
public void onMapReady(GoogleMap googleMap)
{
//Setting map starts here
int LOCATION_ALLOWED = ContextCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
if (LOCATION_ALLOWED == PackageManager.PERMISSION_GRANTED)
{
googleMap.setMyLocationEnabled(true);
}
createLocationRequest();
&#13;
&#34; createLocationRequest()&#34;的实例化发生在需要它的代码块之前,它是否为空?
我已经查看了多个解决方案,但他们都没有帮助我。我希望也许有人可以帮助我,因为这一直困扰着我并且停止了我的应用程序的开发。