我想把地图放大到用户所在的位置,但没起作用
这是一个学校项目。无论我怎么改变量值或命令,它只是加载地图,并且只会往前移动一点点(甚至连放大都没有)。它应该是一个实时跟踪你位置的正在运行的应用,虽然这并非必要,但我希望在打开地图时能把视图放大到用户的位置。
public class Map extends AppCompatActivity {
boolean hasCenteredOnce = false;
MyLocationNewOverlay mLocationOverlay;
MapView map;
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration.getInstance().setUserAgentValue(getPackageName());
setContentView(R.layout.activity_map);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
map = findViewById(R.id.map);
map.setMultiTouchControls(true);
int size = 60;
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawCircle(size / 2f, size / 2f, size / 2.5f, paint);
mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), map);
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableFollowLocation(); // This makes the map follow the user
mLocationOverlay.setDrawAccuracyEnabled(true); // Shows the blue circle accuracy
mLocationOverlay.setPersonIcon(bitmap);
mLocationOverlay.setDirectionIcon(bitmap);
map.getOverlays().add(mLocationOverlay);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = location -> {
double lat = location.getLatitude();
double lon = location.getLongitude();
GeoPoint userPoint = new GeoPoint(lat, lon);
// 2. Only zoom and snap the first time location is found
// This prevents the map from "jumping" while the user is trying to look around
if (!hasCenteredOnce) {
map.getController().setCenter(userPoint);
map.getController().setZoom(17.0);
map.getController().animateTo(userPoint);
hasCenteredOnce = true;
}
};
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableFollowLocation();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
2000,
2,
locationListener);
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
解决方案
不再依赖LocationManager,让MyLocationNewOverlay在首次获得位置信息时通知你。
用下面的代码替换你手动的监听逻辑:
mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), map);
mLocationOverlay.enableMyLocation();
mLocationOverlay.setDrawAccuracyEnabled(true);
mLocationOverlay.setPersonIcon(bitmap);
mLocationOverlay.setDirectionIcon(bitmap);
map.getOverlays().add(mLocationOverlay);
mLocationOverlay.runOnFirstFix(() -> {
runOnUiThread(() -> {
GeoPoint userPoint = mLocationOverlay.getMyLocation();
if (userPoint != null) {
map.getController().setZoom(18.0);
map.getController().animateTo(userPoint);
}
});
});
这只有在GPS实际返回位置信息后才会进行缩放。
另外,
移除enableFollowLocation()。因为你是手动居中:
mLocationOverlay.enableFollowLocation();
删除它,因为它会覆盖你的地图控制器。
此外,
移除LocationManager代码。MyLocationNewOverlay已经在内部处理GPS更新。
所以删除:
locationManager
locationListener
requestLocationUpdates()
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。