我在Android中遇到onLocationChanged事件。这是触发条件:

case R.id.start: {
    Points.add(overlay.getMyLocation()); // Points' type is ArrayList<GeoPoint>
    mgr.requestLocationUpdates(best, 0, 3, locationListener);
    }
    break;

这是onLocationChanged方法:
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。应该在每次位置更改时自动调用它,对吗?就我而言,不是。
请帮我。

最佳答案

问题似乎已经解决。
在onCreate中,我添加了:

Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = mgr.getBestProvider(crit, false);
mgr.requestLocationUpdates(best, 0, 1, locationListener);

onLocationChanged现在看起来像这样:
@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;
}

另外,非常重要的方法:
@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" />

10-08 18:01