我目前正在Flutter中使用GoogleMap。我还使用clustering_google_maps包来显示标记簇。该插件的默认行为是,每次相机移动都会触发所有标记和簇的更新。此行为考虑了用户放大/缩小和翻译。
我在定位包中添加了一个功能,即相机根据用户的位置移动。
initPlatformState() async {
// Wait for the Completer to complete and return the GoogleMap Controler
mapController = await _controller.future;
// Set controller for the ClusteringHelper
clusteringHelper.mapController = mapController;
// Get database and update markers
clusteringHelper.database = await DBHelper().database;
clusteringHelper.updateMap();
// Set parameters for location Service
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH, interval: 1000);
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
// If permission granted, get current location and subscribe to updates
if (_permission) {
LocationData location = await _locationService.getLocation();
final myLocationMarkerId = MarkerId("myLocationMarker");
myLocationMarker = Marker(
markerId: myLocationMarkerId,
position: LatLng(location.latitude, location.longitude),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(
title: "My Location",
snippet: "Latitude: " +
location.latitude.toString() +
" Longitude: " +
location.longitude.toString()),
);
_locationSubscription = _locationService
.onLocationChanged()
.listen((LocationData result) async {
if (cameraUpdateToMyLocation) {
_currentCameraPosition = CameraPosition(
target: LatLng(result.latitude, result.longitude), zoom: 16);
// Safety check if mapController not null
if (mapController != null) {
mapController.animateCamera(
CameraUpdate.newCameraPosition(_currentCameraPosition));
}
}
if (mounted) {
setState(() {
_currentLocation = result;
myLocationMarker = myLocationMarker.copyWith(
positionParam: LatLng(
_currentLocation.latitude, _currentLocation.longitude),
infoWindowParam: InfoWindow(
title: "My Location",
snippet: "Latitude: " +
location.latitude.toString() +
" Longitude: " +
location.longitude.toString())
);
});
}
});
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
initPlatformState();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
error = e.message;
}
}
}
我希望当用户在屏幕上移动地图时,此位置更新停止
不幸的是,当用户移动地图并启动相机动画时,会触发回调onCameraMove
。
只有当用户移动地图时,是否可以触发inCameraMove回调?
Thx!
EDIT当我用手指拖动地图时,onCameraMove
似乎在循环中被调用。。。
据我所知,无法区分onCameraMove回调。你想在这里完成什么?你能更改插件以在CameraIdle回调上显示集群吗?这能解决你的问题吗?
不幸的是,谷歌地图flutter目前不支持此功能。您可以通过在侦听器中包装一个映射来处理它,并控制一个布尔值,该布尔值显示相机移动的动作类型。例如,我想知道相机是通过点击标记移动的(调用标记的点击回调移动相机(还是用户手动拖动地图。每当用户触摸地图时;dragsScreen";布尔值为true,在标记的点击功能中,我将其更改为false,这表明相机的移动是因为标记。所以你可以为任何你想要的功能做这件事。例如,您想知道相机移动是因为调用自己的Special函数还是用户拖动:listener将布尔值("dragsScreen"(更改为true,而该函数将State设置为false。
Listener(
onPointerDown: (PointerDownEvent event){
setState(() {
dragsTheScreen=false;
});
},
child: GoogleMap(
markers: markerItemsForShow.toSet(),
mapType: MapType.normal,
onMapCreated: onMapCreated,
onCameraMove: onCameraMove,
onCameraIdle: onCameraIdle,
initialCameraPosition:CameraPosition(tilt:25,target:LatLng(32.71,51.66), zoom: 5),
),
),
当我创建它们时,在标记点击回调中:
markerItemsForShow.add(Marker(
markerId:...,
position:...,
onTap: (){
setState(() {
dragsTheScreen=true;
});
}
));