2014-10-09 38 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