【问题标题】:How to get more accurate gps readings in android app?如何在 Android 应用程序中获得更准确的 gps 读数?
【发布时间】:2011-10-25 17:06:26
【问题描述】:

我正在使用此代码获取我的应用的位置:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this);

但是当我在我真正的安卓手机上尝试这个应用程序时,它显示的这个位置距离我实际所在的位置大约 80 公里。我如何让这段代码更准确。我希望结果更准确我在做什么..

我正在使用 onLocationChanged 在地图上显示它。它是:

public void onLocationChanged(Location location) {
    if (location != null) {

        //Gets users longitude and latitude
        lat = location.getLatitude();
        lng = location.getLongitude();

        //sets the GeoPoint usersLocation equal lat and lng
        userLocation = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);

        OverlayItem usersLocationIcon = new OverlayItem(userLocation, null, null);
        LocationPin myLocationPin = new LocationPin(userIcon, MainActivity.this);

        //Removes the previous location
        if(previousLocation != null)
               mainMap.getOverlays().remove(previousLocation); 

        myLocationPin.getLocation(usersLocationIcon);
        overlayList.add(myLocationPin);

        //refresh the map
        mainMap.invalidate(); 

        //Making myLocationPin into the previousLocation just to be able to remove it later
        previousLocation = myLocationPin;
    }

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    当来自 GPS 的位置距离上次更新超过 200.0 米时,调用 requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 200.0f, this); 要求更新不超过每 1000 毫秒一次。如果您想要更高的精度,请尝试降低这些数字。

    但是,您不应该离开 80 公里。你是在外面看天空的情况下测试这个吗?

    我认为问题在于四舍五入。您正在使用new GeoPoint((int) lat * 1000000, (int) lng * 1000000);,但改为这样做:

    new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
    

    不同的是,双精度值在乘法之前被转换为整数。这样乘法在之后发生,因此小数点后的数字保持不变。

    【讨论】:

    • 天空的视野很清晰,我试图将 1000 毫秒降低到 500 毫秒,将 200 米降低到 50 米。但它仍然显示距离我真正所在的位置 80 公里。我也试图启动谷歌地图,这显示了我的真实位置..
    • 如何在应用中显示位置?如果您记录纬度和经度值并将它们输入到 Google 地图中,它会将您放在哪里?
    • 我正在使用谷歌地图 API 来显示这个经度和纬度。
    • 你能用你用来做这个的代码编辑你的问题吗?
    • 我在第二部手机上试用了该应用程序,它做了完全相同的事情。它显示我在一个完全不同的城市。
    【解决方案2】:

    有两种可能的答案...

    您可以请求良好的许可,这会使用附近的 wi-fi 网络和 GPS,以便更好地跟踪您所在的位置:

    http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION

    或者您可能只是获得了错误的 GPS 数据。你试过重启手机吗?您是否在 Google 地图中获得了正确的位置?

    希望这会有所帮助。

    【讨论】:

    • 我确实在应用程序中使用了此权限.. 它在谷歌地图中完美运行.. 但我看不到问题.. 有没有我可以添加的代码让它寻找更准确的位置?
    • 您得到的哪些位置不准确?它们只是字符串值,还是显示在您的应用中?
    猜你喜欢
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2021-06-10
    • 2011-09-04
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多