无法使用 LocationRequest 或 getLastLocation() 获取位置
Unable to get location using LocationRequest or getLastLocation()
我研究这个已经有一段时间了。我已经尝试了所有我能想到的。
我正在尝试获取粗略位置(粗略位置)。第一次尝试如下:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
然后是这个:
@Override
public void onConnected(Bundle bundle)
{
lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if(lastKnownLocation == null)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
else
{
moveMap();
}
}
本returns无效。完全有可能,不用担心。但是,在此之后,我尝试使用 LocationRequest 请求位置更新,如下所示:
locationRequest = new LocationRequest();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
然后是:
@Override
public void onLocationChanged(Location location)
{
lastKnownLocation = location;
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
moveMap();
}
这两种方法都没有给我任何位置。这也是可能的,所以我下载了一个假 GPS 应用程序,在 phone 设置中将定位模式设置为仅 GPS,并设置了一个假位置。
当我 运行 我的应用程序执行此操作后,它仍然无法找到位置。我也尝试过:
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
似乎没有任何效果。权限在清单中。我做错了什么?
您这样做是为了开始获取位置更新吗?
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
请检查这个。
通过执行以下操作修复了它:
删除了这个:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
添加了这个:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
并将 connect() 调用移动到 onStart()。
我认为问题是缺少 addConnectionCallbacks。
我研究这个已经有一段时间了。我已经尝试了所有我能想到的。
我正在尝试获取粗略位置(粗略位置)。第一次尝试如下:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
然后是这个:
@Override
public void onConnected(Bundle bundle)
{
lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if(lastKnownLocation == null)
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
else
{
moveMap();
}
}
本returns无效。完全有可能,不用担心。但是,在此之后,我尝试使用 LocationRequest 请求位置更新,如下所示:
locationRequest = new LocationRequest();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
然后是:
@Override
public void onLocationChanged(Location location)
{
lastKnownLocation = location;
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
moveMap();
}
这两种方法都没有给我任何位置。这也是可能的,所以我下载了一个假 GPS 应用程序,在 phone 设置中将定位模式设置为仅 GPS,并设置了一个假位置。
当我 运行 我的应用程序执行此操作后,它仍然无法找到位置。我也尝试过:
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
似乎没有任何效果。权限在清单中。我做错了什么?
您这样做是为了开始获取位置更新吗?
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
请检查这个
通过执行以下操作修复了它:
删除了这个:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
添加了这个:
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addApi(Places.GEO_DATA_API)
.addApi(LocationServices.API)
.build();
并将 connect() 调用移动到 onStart()。
我认为问题是缺少 addConnectionCallbacks。