我已经完成了这个代码,我想获得经度和纬度,但是使用网络提供者和gps提供者我得到的位置为null。即使gps已启用......为什么会这样?
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean isNetworkEnabled = lm .isProviderEnabled(LocationManager.NETWORK_PROVIDER); Location location = null; if (!isGPSEnabled && !isNetworkEnabled) { } else if(isGPSEnabled ) { location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location == null) { if(isNetworkEnabled) location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location == null) { } } } else if(isNetworkEnabled){ location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location == null) { } }
答案 0 :(得分:0)
由于您的gps
已启用(remember gps takes alot of time to get coordinates
)并且您在代码中首先检查gps,因此有几个原因导致您没有获得坐标,而是因为您的network
if (!isGPSEnabled && !isNetworkEnabled)
{
}
else if(isNetworkEnabled){
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location == null)
{
}
}
else if(isGPSEnabled ) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location == null)
{
if(isNetworkEnabled)
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location == null)
{
}
}
}
部分是因为您正在使用if else.so将您的代码更改为这样并首先检查网络。
{{1}}