0

我已经在想要获取经度和纬度的地方完成了这段代码,但是使用网络提供商和 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)
            {
            }
        }
4

1 回答 1

0

没有获得坐标的原因有几个,但如果是您的,因为您gps已启用(remember gps takes alot of time to get coordinates)并且您首先在代码中检查 gps,它不会转到该network部分,因为您正在使用 if else.so 将您的代码更改为像这样,先检查网络。

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)
                {
                }
            }
        } 
于 2014-03-26T11:55:31.427 回答