我正在制作添加了Google Maps
的导航地图。它的工作方式是,一旦用户GPS /位置更改,地图便会动画显示到当前位置标记位置。它运作良好,但是一旦我手动移动地图以沿多段线查看目标标记,该地图也会自动动画化回用户位置。如何确保动画仅在用户位置更改时发生,而不是在我自己移动地图时发生。
void updatePinOnMap() async {
// create a new CameraPosition instance
// every time the location changes, so the camera
// follows the pin as it moves with an animation
CameraPosition cPosition = CameraPosition(
zoom: CAMERA_ZOOM,
tilt: CAMERA_TILT,
bearing: currentLocation.heading,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
);
final GoogleMapController controller = await _controller.future;
///here i need an if statement to only animate when GPS changed not when i move/slide the map.
controller.animateCamera(CameraUpdate.newCameraPosition(cPosition));
// do this inside the setState() so Flutter gets notified
// that a widget update is due
setState(() {
// updated position
var pinPosition =
LatLng(currentLocation.latitude, currentLocation.longitude);
// the trick is to remove the marker (by id)
// and add it again at the updated location
_markers.removeWhere((m) => m.markerId.value == 'sourcePin');
_markers.add(Marker(
markerId: MarkerId('sourcePin'),
position: pinPosition, // updated position
icon: sourceIcon));
});
}