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只调用一次,只有在我preSS开始。它应该是每一个位置发生了变化,适当的时候会自动调用?就我而言,事实并非如此。
请帮我。
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);
}
也有一些新的权限:
And also some new permissions:
<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" />