【发布时间】:2015-05-07 07:36:33
【问题描述】:
我环顾四周,但找不到解决方案。我试图在 Android 中找到最后一个已知位置,我从谷歌开发人员那里看到了这个,但函数 onConnected() 从未被调用过。另外,如果我把它放在onCreate() 中,Android 的地理定位图标永远不会打开。
该应用使用已使用 FusedLocation 的权限和谷歌服务。
//class implements ConnectionCallbacks, OnConnectionFailedListener
protected void onCreate(Bundle savedInstanceState) {
[...]
buildGoogleApiClient();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
String lat = String.valueOf(mLastLocation.getLatitude()); //it's only for test
String lng = String.valueOf(mLastLocation.getLongitude());
}
}
编辑:没关系,休息一天后,该功能运行正常。代码是一样的。我要删除这个问题吗?
【问题讨论】:
标签: android google-play-services android-location google-api-client