gpt4 book ai didi

android - onLocationChanged 不会自动调用

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:35 26 4
gpt4 key购买 nike

我对 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" />

关于android - onLocationChanged 不会自动调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8447861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com