本文介绍了onLocationChanged 不会自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Android 中的 onLocationChanged 事件有疑问.这是触发:
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 仅在我按下开始"后才被调用一次.它应该在每次位置更改时自动调用,对吗?在我的情况下,它不是.
请帮帮我.
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.
推荐答案
问题似乎解决了.在 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 现在看起来像这样:
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);
}
还有一些新的权限:
<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" />
这篇关于onLocationChanged 不会自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!