我正在尝试制作一个使用地图的应用程序,该应用程序将地图的中心保持在您的位置,但是当您在地图上四处移动时,它不会自动向后移动。我一直在尝试使其工作一段时间,并尝试了不同的方法。
使用我当前的代码,即使我移动地图,地图也会自动回到中心。
int swipe = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mo = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location");
if (Build.VERSION.SDK_INT >= 23 && !isPermissionGranted()) {
requestPermissions(PERMISSIONS, PERMISSION_ALL);
} else requestLocation();
if (!isLocationEnabled())
showAlert(1);
}
public boolean onTouch(View v, MotionEvent me) {
swipe = 2;
return false;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
marker = mMap.addMarker(mo);
}
@Override
public void onLocationChanged(Location location) {
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
marker.setPosition(myCoordinates);
if (swipe == 1) {
CameraUpdate center = CameraUpdateFactory.newLatLng(myCoordinates);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15.0f);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
} else {
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(100);
}
}