2011-09-26 83 views
2

我需要用户的确切位置。所以,我创建了两个听众为:Android获得最佳位置

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mNetworkListener); 

我倾听来自网络和GPS提供商位置更新,并要检查的双方获得的位置准确性因素,这样我可以挑准确位置。我只是想问我是否使用正确的方式来实现...... ???如果没有,请提供一些指导方针如何做到这一点...... ???

我的示例代码:

if(MyGPSListener.currentLocation != null) { 
     if(MyNetworkListener.currentLocation != null) { 
      if(MyGPSListener.currentLocation.getAccuracy() <= MyNetworkListener.currentLocation.getAccuracy()) { 
       useGPSLocation(); 

      } 
      else if(MyGPSListener.currentLocation.getAccuracy() > MyNetworkListener.currentLocation.getAccuracy()) { 
       useNetworkLocation(); 

      } 
     } 
     else { 
      useGPSLocation(); 

     } 

    } 
    else if(MyNetworkListener.currentLocation != null){ 
     useNetworkLocation(); 

    } 
+0

为什么不试试这个 http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location- in/an/3145655#3145655 –

回答

3

你应该看看:http://developer.android.com/guide/topics/location/obtaining-user-location.html 特别的功能:isBetterLocation

我可能会使用类似isBetterLocation。因为实际位置更新有多大是很重要的。至少如果你真的想知道用户在哪里知道,而不是用户在哪里。

但是你可以肯定地执行你自己的功能,你描述的时间检查。

+0

你能否详细说明如何在我的情况下使用isBetterLocation(...)...? –

+0

那么首先你需要包含更多的代码,因为我只看到一小部分。但是当你得到的位置,你可以调用isBetterLocation新的位置和你已经保存的旧位置。如果最好使用它,如果不是不使用该位置。 –

+0

非常感谢... –