如何在Android应用程序中获得更准确的GPS读数?

时间:2011-10-25 17:06:27

标签: android android-location

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

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

但是当我在真正的Android手机中尝试使用该应用程序时,它显示了距离该位置约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;
    }

2 个答案:

答案 0 :(得分:4)

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

不同之处在于,在乘法之前,双值被转换为整数。这样,乘法就会发生,所以小数点后的数字就会保持不变。

答案 1 :(得分:0)

有两种可能的答案......

你可以要求获得许可,这会使用附近的Wi-Fi网络和GPS,以便更好地跟踪你的位置:

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

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

希望这有帮助。