我正在使用Google V2,我试图获得当前的location.But,它总是返回null。
码
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider); 每次我得到location零.My全球定位系统是好的工作在我的设备,有什么问题,请帮助我。
发布于 2014-02-28 07:32:16
根据getLastKnownLocation文档
如果当前禁用了提供程序,则返回null。
在使用getBestProvider(criteria, false)时,您是说允许未启用的提供程序(这就是false的意思)--如果您只想查看已启用的提供程序(这将确保getLastKnownLocation不返回null),则切换到true。
请注意,getLastKnownLocation可能非常过时,如果需要获得最近的位置,您可能仍然希望查找位置更新。
发布于 2014-02-28 08:26:36
有不同的方法。首先,请确保您在清单中拥有所有所需的许可:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />如果你只想要一个近似的位置,你可以得到最后的位置(GPS或网络)。
if(_locationManager != null)
{
if(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null)
return new Location(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER),true);
else if(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null)
return new Location(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER),true);
}https://stackoverflow.com/questions/22088929
复制相似问题