纬度不准确,但龙是准确的 - Android设备

时间:2014-10-09 03:23:59

标签: c# android google-maps gps

我试图通过GPS从Android设备上获取我的LatLong。

有我的代码:

    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);
            }

}

我发送我的lat,长到c#代码图(动态数据显示)并在地图上绘制点。 代码错误的lat(它靠近我的位置但不准确。 来自addressString的虽然,我准确地得到了建筑物,城市和国家的地址。 addressString来自gps,Lat long来自GPS,为什么它也准确呢?

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

我重新开始,只有拉特错了,长的是完美的。 我会非常感谢,我会找到答案,这对我来说非常重要。感谢。

1 个答案:

答案 0 :(得分: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);