我製作了一個Android應用,通過點擊按鈕獲取經緯度位置。Android GPS需要一段時間才能準確
起初,我得到最後一次已知的位置讀數,爲了參數的緣故,當我第一次載入應用程序/開啓GPS時,這是不準確的。
我想知道的是如何等待它是準確的,就像在谷歌地圖上,當你得到敬酒信息'等待位置'時。
如果你看到任何方式的代碼可以改進,它也將是有幫助的。
代碼以供參考:
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();
}
}
感謝您的閱讀,所有答覆將不勝感激。
本
想也許使用所有的供應商,而不是最好的供應商?它不一定非常準確。 –