安卓GPS精度甚至不接近内置地图应用程序



我已经尝试了Android SDK几个星期了,试图从后台服务中获得准确的位置。

在尝试了一些配置后,我目前有一个循环:

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
bestProvider = locationManager.getBestProvider(criteria, true);
lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);

然后我时不时地检查lastKnownLocation以获取位置更新。我知道你可以听更新,但目前我不太担心。我担心的是,(我认为)我要求手机尽可能使用 GPS - 而不是其他确定位置的方法 - 但它仍然从很远的距离返回纬度/经度,但当我打开地图应用程序时,它让我在几米之内。

谁能建议我在这里出错的地方?

设置标准只是根据它们确定最好使用哪个提供程序,这样就不会真正对位置的准确性或有效性发表意见。我只是立即将提供商设置为 GPS(如果 GPS 可用!

此外,您似乎也没有根据时间和距离对更新之前要等待多长时间的任何要求。以下是我使用意图和广播接收器所做的事情的示例。它可能会对你有所帮助。

public void beginMonitoringLocation(int minDistance) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(MainActivity.LOCATION_UPDATE_ACTION);
            this.mContext.registerReceiver(this.locationReceiver, filter);
            LocationManager mLocationManager = (LocationManager) this.mContext.getSystemService(Context.LOCATION_SERVICE);
            mLocationManager.addGpsStatusListener(this);
            boolean enabled =  mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (!enabled) {
                Log.e("LocationManager", "GPS not enabled!!!!");
            } 
            LocationProvider provider = mLocationManager.getProvider(LocationManager.GPS_PROVIDER); // GET THE BEST PROVIDER FOR OUR LOCATION
            Log.d("LocationManager:","Location Provider:"+provider);
            if ( provider == null ) {
                Log.e( "LocationManager", "No location provider found!" );
                return;
            }
            final int locationUpdateRC=0;
            int flags = 0;
            Intent intent = new Intent(MainActivity.LOCATION_UPDATE_ACTION); 
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.mContext, locationUpdateRC, intent, flags); 
            // PENDING INTENT TO BE FIRED WHEN THE LOCATIONMANAGER RECEIVES LOCATION UPDATE.
            // THIS PENDING INTENT IS CAUGHT BY OUR BROADCAST RECEIVER
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,minDistance,pendingIntent);
            this._monitoringLocation = true;
    }

然后在同一堂课上,我把我的broadcast receiver

public BroadcastReceiver locationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Location location = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
        if (location != null) {
            //Do something with it  
        }
        }
    };

我的意图过滤器的操作只是对活动中设置的常量的静态引用。

public static final String LOCATION_UPDATE_ACTION = "com.corecoders.sqlmaptrack.LOCATION_UPDATE_RECEIVED";

这在我的情况下为我提供了准确的位置。如果需要,您可以将距离设置为 0,然后您会发现,如果您有 4 颗或更多卫星的良好定位,您将获得每秒 5 个精度的位置定位。

我希望这对你有帮助

我使用以下代码来获取准确的位置。使用以下代码,您可以通过编程方式启用/禁用 GPS。

private void enableGPSTracking() {
            new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    toggleGPS(true, getApplicationContext());
                    provider = LocationManager.GPS_PROVIDER;
                    Criteria locationCritera = new Criteria();
                    locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
                    locationCritera.setAltitudeRequired(false);
                    locationCritera.setBearingRequired(false);
                    locationCritera.setCostAllowed(true);                   locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
                    provider = locationManager.getBestProvider(locationCritera,
                            true);
                    Intent intent = new Intent(
                            "com.example.gps.LOCATION_READY");
                    pendingIntent = PendingIntent.getBroadcast(
                            getApplicationContext(), 0, intent,
                            PendingIntent.FLAG_CANCEL_CURRENT);
                    // Register for broadcast intents
                    locationManager.requestLocationUpdates(provider, 0, 0,
                            pendingIntent);
                } catch (Exception e) {
                }
            }
        }.start();
    }
public static void toggleGPS(boolean flag, Context context) {
        try {
            Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.putExtra("enabled", flag);
            context.sendBroadcast(intent);
        } catch (Exception e) {
        }
    }

要禁用 GPS,只需为 ToggleGPS() 方法的标志值传递 false。希望这对你有帮助。

最新更新