Android Gps应用程序不如谷歌地图应用程序

时间:2016-05-10 20:32:26

标签: android gps location maps

我正在开发一款需要随时了解您所在位置的应用。我知道位置准确性并不完美,我相信我已经尝试了谷歌开发者帮助,stackoverflow等所发现的一切......

我遇到的问题如下: 在某些地方,我得到的位置不如使用谷歌地图获得的位置准确。我使用网络提供商和gps提供商来更新位置,我会过滤它们以保持最准确的位置(使用谷歌开发者提供的代码)。

在大多数地方,准确性会很好,接近完美,但在某些地区,我的准确度非常差,实际位置与gps /网络提供的差异高达100米。

我发布下面的代码,我想知道是否还有其他方法可以提高准确性。

private void setLocationListener() {
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            if(isBetterLocation(location,miLocation)) {
                miLocation=location;
                doStuff(location);
            }

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(MapActivity2.this,"Ha desactivado el gps",Toast.LENGTH_LONG).show();
            MapActivity2.this.finish();

        }


    };
    checkCallingPermission(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}


protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }
//        if (location.getAccuracy()>15) return false;

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TIEMPO_LOC;
    boolean isSignificantlyOlder = timeDelta < -TIEMPO_LOC;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}

private void doStuff(Location location) {
    if (myMarker != null) miMarker.remove();
    int iconResource = getResources().getIdentifier("iconomylocation", "drawable", getPackageName());
    myMarker = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lng))
            .title("Our location")
            .icon(BitmapDescriptorFactory.fromResource(iconResource)));
}

TIEMPO_LOC是1000; (1秒)我真的不能放慢速度。

编辑:哦忘了提及,我来自西班牙(不应该像其他用户那样的中国问题有任何抵消)。

0 个答案:

没有答案