問(wèn)題描述
我對(duì) Android 中的 onLocationChanged 事件有疑問(wèn).這是觸發(fā):
I have a problem with onLocationChanged event in Android. Here's the triggering:
case R.id.start: {
Points.add(overlay.getMyLocation()); // Points' type is ArrayList<GeoPoint>
mgr.requestLocationUpdates(best, 0, 3, locationListener);
}
break;
這是 onLocationChanged 方法:
And here's the onLocationChanged method:
public void onLocationChanged(Location location) {
i++;
Points.add(overlay.getMyLocation());
MapOverlay mapOverlay = new MapOverlay(Points.get(i-1), Points.get(i));
map.getOverlays().add(mapOverlay); //does the drawing
mMapController.animateTo(Points.get(i));
}
因此,onLocationChanged 僅在我按下開(kāi)始"后才被調(diào)用一次.它應(yīng)該在每次位置更改時(shí)自動(dòng)調(diào)用,對(duì)嗎?在我的情況下,它不是.
請(qǐng)幫幫我.
So, onLocationChanged is called only once and only after I press "start". It's supposed to be called automatically every time the location has changed, right? In my case, it's not.
Please help me.
推薦答案
問(wèn)題似乎解決了.在 onCreate 中,我添加了:
Problem seems to be solved. In onCreate, I added:
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = mgr.getBestProvider(crit, false);
mgr.requestLocationUpdates(best, 0, 1, locationListener);
onLocationChanged 現(xiàn)在看起來(lái)像這樣:
onLocationChanged now looks like that:
@Override
public void onLocationChanged(Location location) {
i++;
nextPoint = overlay.getMyLocation();
latitude = nextPoint.getLatitudeE6();
longtitude = nextPoint.getLongitudeE6();
lastPoint = new GeoPoint((int) latitude, (int) longtitude);
Points.add(lastPoint);
MapOverlay mapOverlay = new MapOverlay(Points.get(i - 1), Points.get(i));
map.getOverlays().add(mapOverlay);
mMapController.animateTo(Points.get(i));
nextPoint = null;
lastPoint = null;
}
另外,非常重要的方法:
Also, very important methods:
@Override
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 10000, 1, locationListener);
}
@Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(locationListener);
}
還有一些新的權(quán)限:
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
這篇關(guān)于onLocationChanged 不會(huì)自動(dòng)調(diào)用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!