我已经尝试 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 - 而不是其他确定位置的方法 - 但它仍然从很远的距离返回纬度/经度,但当我打开 map 应用程序,它距离我只有几米。
有人可以建议我哪里出错了吗?
最佳答案
设置标准只是根据它们确定最好使用哪个提供商,因此对位置的准确性或有效性没有真正的发言权。我只是立即将提供商设置为 GPS(如果 GPS 可用!)。
此外,您似乎没有给它任何关于根据时间和距离更新之前要等待多长时间的要求。这是我使用 Intent 和广播接收器所做的示例。它可能对你有帮助。
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;
}
然后在同一个类中我放置了我的广播接收器
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
}
}
};
我的 Intent 过滤器的操作只是对我的 Activity 中设置的常量的静态引用。
public static final String LOCATION_UPDATE_ACTION = "com.corecoders.sqlmaptrack.LOCATION_UPDATE_RECEIVED";
这对我来说很有效,为我提供了准确的位置。如果您愿意,您可以将距离设置为 0,然后您会发现,如果您有 4 颗或更多卫星的良好定位,您将获得每秒 5 次准确度的位置定位。
希望对你有帮助
关于Android GPS 精度甚至不及内置 map 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911105/