2014-03-26 113 views
0

我已经完成了这个代码,我想获得经度和纬度,但同时使用网络提供商和GPS提供商我得到我的位置为空。即使GPS启用...为什么这样?Android网络提供商和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

重复的问题 http://stackoverflow.com/questions/12597076/network-provider-and-gps-provider-returning-null-values?rq=1 – Tugrul

+0

也有一些是在Android的新版本。 你使用的是设备还是仿真器。如果设备是什么版本。 –

+0

它的索尼手机 – user3278732

回答

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) 
       { 
       } 
      } 
     } 
+0

我的错误似乎是在索尼手机上,它有什么不同吗? 在所有情况下,我正在创建0 - 0 .. – user3278732