【发布时间】:2012-01-16 16:32:32
【问题描述】:
我制作了一个 Android 应用程序,通过单击按钮获取经度和纬度位置。
起初我得到最后一个已知的位置读数,为了论证,当我第一次加载应用程序/打开 gps 时,它是不准确的。
我想知道的是如何等待它准确无误,就像在 Google 地图中当您收到 toast 消息“等待位置”时一样。
如果您看到任何可以改进代码的方法,那也会很有帮助。
参考代码:
public class Clue extends Activity {
public static double latitude;
public static double longitude;
Criteria criteria;
LocationManager lm;
LocationListener ll;
Location location;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener();
lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll);
}
private boolean weAreThere() {
location = getLocation();
longitude = location.getLongitude();
latitude = location.getLatitude();
return inCorrectPlace(param);
}
private Location getLocation() {
lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll);
return lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
}
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
Clue.longitude = loc.getLongitude();
Clue.latitude = loc.getLatitude();
}
}
感谢您的阅读,我们将不胜感激。
本
【问题讨论】:
-
是否考虑过使用所有提供者而不是最好的提供者?它不必非常准确。
标签: java android gps location locationmanager