我有这个代码来获得最好的供应商
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
Location mostRecentLocation = lm.getLastKnownLocation(provider);
if(mostRecentLocation != null) {
latid=mostRecentLocation.getLatitude();
longid=mostRecentLocation.getLongitude();
}
lm.requestLocationUpdates(provider, 1, 0, locationListener);
然后是听众
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
latid = loc.getLatitude();
longid = loc.getLongitude();
// if(loc.hasAccuracy()==true){
accuracyd = loc.getAccuracy();
String providershown = loc.getProvider();
accuracy.setText("Location Acquired. Accuracy:"
+ Double.toString(accuracyd) + "m\nProvider: "+providershown);
accuracy.setBackgroundColor(Color.GREEN);
// }
userinfo=usernamevalue+"&"+Double.toString(latid)+"&"+Double.toString(longid);
submituserlocation(userinfo);
}
}
当我将它测试到设备(htc magic)时,我发现当 gps 被禁用时,它会立即从网络中锁定。当我启用 gps 时,它不会从网络获取任何数据并等待它从 gps 锁定。
我想像谷歌地图一样锁定位置,直到他们有良好的 gps 信号,他们才会使用网络来确定我的位置。
我虽然最好的标准可以做到这一点,但他们所做的只是选择一次提供商。
我的代码有问题还是我必须做线程和超时等才能实现?