gpt4 book ai didi

android - LocationServices.FusedLocationApi.getLastLocation 在 onCreate() 方法中不起作用

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

这是我使用相同方法的代码 - 一次是在我的 MainActivity 中的 onCreate() 期间,一次是在用户单击按钮后

// Below code not working during onCreate
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

// Create an instance of GoogleAPIClient. From Google API demo code
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

startLocationOnScreen.setText(getCurrentLocationViaPhoneLocation());

// Surprisingly same method works if it's called after a button press!

startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

startLocationOnScreen.setText(getCurrentLocationViaPhoneLocation());

那为什么会这样呢?我基本上想在加载应用程序时获取用户的当前位置,而不是强制用户按下按钮来获取相同的位置。

getCurrentLocationViaPhoneLocation() 方法的实现——(大部分取自 Google API 文档)

protected String getCurrentLocationViaPhoneLocation() {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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 "Error - location services not available!";
}
startLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (startLocation != null) {
Log.e("We are at ", String.valueOf(startLocation.getLatitude()));
Log.e("We are at ", String.valueOf(startLocation.getLongitude()));

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

try {
List<Address> addressList = geocoder.getFromLocation(startLocation.getLatitude(), startLocation.getLongitude(), 1);

if (addressList != null && addressList.size() > 0) {


currentCity = addressList.get(0).getLocality();


Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}

return (sb.toString());

}

} catch (IOException e) {
e.printStackTrace();
}


}

return ("Error - current location unavailable!");
}

编辑: 我得到: Error - current location unavailable! during onCreate() 这意味着 startLocation==null 当方法在 onCreate() 期间被调用时。

最佳答案

我怀疑在您第一次调用该方法时(即在 onCreate 期间)GoogleApiClient 尚未连接。

您已经调用了 Builder 的 addConnectionCallbacks 方法。因此,您应该在 Activity 中实现 GoogleApiClient.ConnectionCallbacksonConnected(Bundle) 方法。在这里而不是在 onCreated 中调用 getCurrentLocationViaPhoneLocation 方法,然后您可以确定 GoogleApiClient 已正确连接。

如果 onConnected 方法没有被调用,尝试在 onCreate 中创建实例后添加 mGoogleApiClient.connect()

希望对您有所帮助!

关于android - LocationServices.FusedLocationApi.getLastLocation 在 onCreate() 方法中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398115/

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