2012-04-24 103 views
3

`私人無效的getLocation(){獲取GPS定位精確

 String context = Context.LOCATION_SERVICE; 
     LocationManager locationManager = (LocationManager)getSystemService(context); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 

     String provider = locationManager.getBestProvider(criteria, true); 
     Location location = locationManager.getLastKnownLocation(provider); 
     updateWithNewLocation(location); 

     locationManager.requestLocationUpdates(provider, 2000, 5, locationListener); 

    } 

    private final LocationListener locationListener=new LocationListener() 
    { 
     public void onLocationChanged(Location location) 
     { 
      updateWithNewLocation(location); 
     } 
     public void onProviderDisabled(String provider) 
     { 
      updateWithNewLocation(null); 
     } 
     public void onProviderEnabled(String provider) 
     { 

     } 
     public void onStatusChanged(String provider,int status,Bundle extras) 
     { 

     } 
    }` 

我爲獲得上述地點的代碼。但我的申請結果並不準確。我想確切地知道我的位置。有沒有其他方法?

回答

4

準確定位取決於信號是否可用。例如。如果gps信號存在或不存在。如果GPS不存在,電話使用wifi或服務提供商跟蹤,這是不準確的。因此,請嘗試使用Google地圖並查看您的位置,如果您使用Google地圖獲得更準確的位置修正,那麼您在代碼中缺少某些內容。

我看到的一個錯誤是您正在使用ACCURACY_COARSE,請嘗試使用ACCURACY_FINE。如果你還沒有,你也必須在清單中添加這些權限。

+0

如果我使用ACCURACY_FINE,我不能得到我的位置:( – thanhtv 2012-04-24 07:28:23

+0

是的,最有可能的地方,你正在測試它,並沒有很好地捕捉到GPS信號。谷歌地圖能夠提供一個更好的位置在那個地方比 – 2012-04-24 23:13:41

+0

COURSE設置使用wifi或位置提供商,這可能會給你的位置更快,將使用較少的電池,因爲你沒有使用GPS設備,但它會更不準確,但如果你需要準確您希望使用通過FINE的GPS設備的位置。 – 2012-04-24 23:21:47