gpt4 book ai didi

android - LocationServices.FusedLocationApi.getLastLocation(googleApiClient) 在模拟器上始终为 null

转载 作者:行者123 更新时间:2023-11-29 00:04:01 24 4
gpt4 key购买 nike

我不知道模拟器出了什么问题。我无法使用此命令进行定位

LocationServices.FusedLocationApi.getLastLocation(googleApiClient)

在我的模拟器中,支持 gps 和 google play 服务,但无法获取当前位置。位置 = 空。需要帮忙。这是我的代码:

  private void configureGoogleApiClient() {
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(Constant.INTERVAL);
locationRequest.setFastestInterval(Constant.FASTINTERVAL);

googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (googleApiClient != null) {
googleApiClient.connect();
}
}

@Override
protected void onStart() {
super.onStart();
if (googleApiClient != null) {
googleApiClient.connect();
}
}

protected void onStop() {
// Disconnecting the client invalidates it.
if (googleApiClient != null) {
googleApiClient.disconnect();
}
super.onStop();
}


@Override
protected void onPause() {
super.onPause();
if (googleApiClient != null) {
googleApiClient.disconnect();
}
}

@Override
protected void onResume() {
super.onResume();
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if (googleApiClient != null)
googleApiClient.connect();
}
}

@Override
public void onConnected(@Nullable Bundle bundle) {
if (googleApiClient.isConnected()) {
Log.e(MainActivity.class.getSimpleName(), "Is connected");
} else
Log.e(MainActivity.class.getSimpleName(), "Is not connected");

if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.e(MainActivity.class.getSimpleName(), "Is connected on GPS");
} else {
Log.e(MainActivity.class.getSimpleName(), "Is not connected on GPS");
}

Utility.checkPermission(this);
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

if (location == null) {
Utility.checkPermission(this);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

if (location != null) {
latText.setText("Lat: " + String.valueOf(location.getLatitude()));
longText.setText("Long: " + String.valueOf(location.getLongitude()));
} else {
latText.setText(getString(R.string.error_find_location));
longText.setText(getString(R.string.error_find_location));
}
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(MainActivity.class.getSimpleName(), connectionResult.toString());
}

@Override
public void onLocationChanged(Location location) {
currentLocation = location;
if (googleApiClient != null)
if (googleApiClient.isConnected() || googleApiClient.isConnecting()) {
googleApiClient.disconnect();
googleApiClient.connect();
} else if (!googleApiClient.isConnected()) {
googleApiClient.connect();
}
String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
configureView();
}

最佳答案

我的猜测是最后一个位置只显示最后一个位置。它实际上并不试图找出位置。在您的手机上,另一个应用程序可能请求了该位置。所以,还有最后一个位置。

如果没有位置,我已添加此代码以请求位置。

Location userLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (userLocation == null) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setNumUpdates(1);
locationRequest.setInterval(0);
locationRequest.setPriority(PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest,
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient,
this);
}
});

关于android - LocationServices.FusedLocationApi.getLastLocation(googleApiClient) 在模拟器上始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com