2014-10-09 50 views
0

我試圖讓我的LatLong從Android設備與GPS。緯度不準確,但龍是準確的 - Android設備

有我的代碼:

LocationManager lm,locationManager; 
    Location l; 
    TextView lt, ln; 
    lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     l=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     updateWithNewLocation(l); 

     private void updateWithNewLocation(Location location) { 
       String latLongString = "Unknown"; 
       String addressString = "No address found"; 
       DecimalFormat df = new DecimalFormat("##.00"); 
       if (location != null) { 
        double lat = location.getLatitude(); 
        double lng = location.getLongitude(); 
        latLongString = "Lat:" + df.format(lat) + "\nLong:" + df.format(lng); 
        Geocoder gc = new Geocoder(MainActivity.this, Locale.getDefault()); 
        try { 
         List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
         if (addresses.size() == 1) { 
          addressString=""; 
          Address address = addresses.get(0); 
          addressString = addressString + address.getAddressLine(0) + "\n"; 
          for (int i = 0; i < address.getMaxAddressLineIndex(); i++){ 
           addressString = addressString + address.getAddressLine(i) + "\n"; 
          } 
          addressString = addressString + address.getLocality()+ "\n"; 
          addressString = addressString + address.getPostalCode()+ "\n"; 
          addressString = addressString + address.getCountryName()+ "\n"; 
         } 
        } catch (IOException ioe) { 
         Log.e("Geocoder IOException exception: ", ioe.getMessage()); 
        } 
       } 
       accurity.setText("Your Current Position is:\n" + latLongString + "\n" + addressString); 
       ln.setText(""+lng); 
       lt.setText(""+lat); 
      } 

} 

我送我的緯度,經度,以C#代碼映射(動態數據顯示),並在地圖上繪製點。 緯度是錯誤的代碼(它接近我的位置,但不準確 雖然 from addressString我得到了準確的我的地址的建設和城市和國家 從gps和緯度得到的地址長得到從GPS,爲什麼它準確嗎?

addresses = gc.getFromLocation(lat, lng, 1); 

林repit,只有LAT是錯誤的,長的是完美的。 我會如此心存感激的人會發現這個答案是非常重要的謝謝

+0

的LocationManager,尤其是在使用GPS提供商只,需要相當長的時間來獲得高精確度(lat,lng)對。你可以檢查精確度(location.getAccuracy())並等到它足夠小。融合了GPS,WiFi,Cell,融合的位置提供商,??? (谷歌不會說)更快,更省電。看一看! – 2014-10-09 03:48:23

+0

謝謝你,但是我能做些什麼來改善我的生活?看我的編輯。長期是完美的,但緯度不是.. – 2014-10-09 03:49:55

回答

0

lm.getlastlocation並不總是給出精確更新的lat long。您需要在requestlocationupdate(locationrequest,location_listener)中使用位置請求對象,並使用提供位置偵聽器的lat long來提供準確的結果。共享代碼如下:

private LocationRequest mlocationrequest; 


// location listener 
LocationListener loc_listener = new LocationListener() 
{ 

    @Override 
    public void onLocationChanged(Location arg0) 
    { 

     if (arg0 != null) 
     { 
      lat = arg0.getLatitude(); 
      lng = arg0.getLongitude(); 
      tm = arg0.getTime(); 
     } 

    } 
}; 

而在你的上創建方法

mlocationrequest = LocationRequest.create(); 
    mlocationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mlocationrequest.setFastestInterval(1000); 
    mlocationrequest.setInterval(10000); 

然後簡單的通過

mlocationclient.requestLocationUpdates(mlocationrequest, loc_listener); 
+0

什麼是locationRequest?它不存在於日食..它沒有給它任何導入.. – 2014-10-09 04:04:11

+0

你需要使用谷歌更新的位置api,它在developer.android.com上可用,新的api與谷歌播放服務本身集成 – 2014-10-09 04:05:27

+0

研究這第一個: https://developer.android.com/google/play-services/location.html – 2014-10-09 04:06:49