Flutter版本:3.0.2Dart版本:2.17.3
包:google_maps_flutter: ^ 2.1.8
GoogleMap小部件示例代码:
Completer<GoogleMapController> _controllerGoogleMap = Completer();
GoogleMapController? _googleMapController;
Stack(
children: [
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(12.9716, 77.5946),
zoom: 18,
),
minMaxZoomPreference:
MinMaxZoomPreference(8, 19),
zoomControlsEnabled: false,
zoomGesturesEnabled: true,
scrollGesturesEnabled: true,
mapToolbarEnabled: false,
rotateGesturesEnabled: false,
tiltGesturesEnabled: false,
myLocationEnabled: true,
mapType: MapType.normal,
compassEnabled: false,
onMapCreated: onMapCreated,
onCameraMove: onCameraMove,
onCameraIdle: onCameraIdle,
gestureRecognizers: Set()
..add(Factory<PanGestureRecognizer>(
() => PanGestureRecognizer())),
),
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Container(
width: width * 0.15,
height: width * 0.05,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
width: 2,
color: Colors.black),
shape: BoxShape.circle),
child: Padding(
padding:
const EdgeInsets.all(2.0),
child: Container(
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle),
),
),
),
Container(
width: width * 0.006,
height: height * 0.025,
decoration: BoxDecoration(
borderRadius:
new BorderRadius.only(
bottomLeft:
const Radius.circular(
5.0),
bottomRight:
const Radius.circular(
5.0),
),
color: Colors.black),
),
SizedBox(
height: 30,
),
],
),
),
]),
_onMapCreated(GoogleMapController controller){
_controllerGoogleMap.complete(controller);
_googleMapController = controller;
}
onCameraMove(){
print("On Camera Move method");}
onCameraIdle(){
print("On Camera Idle method");}
问题:当尝试缩放谷歌地图时,它调用oncamermove方法和onCameraIdle方法并放大或缩小不是基于当前位置。
那么,当我们放大/缩小谷歌地图时,如何停止调用oncamermove方法和onCameraIdle方法
检查了提供的代码片段后,这里是工作代码片段:
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MarkerMapWidget extends StatelessWidget {
final Completer<GoogleMapController> _controllerGoogleMap = Completer();
final ValueNotifier<CameraPosition> currentCameraPosition = ValueNotifier(
const CameraPosition(target: LatLng(12.9716, 77.5946), zoom: 18));
MarkerMapWidget({Key? key}) : super(key: key);
_onMapCreated(GoogleMapController controller) {
_controllerGoogleMap.complete(controller);
}
onCameraMove(CameraPosition position) {
print("On Camera Move method $position");
currentCameraPosition.value = position;
}
onCameraIdle() {
print("On Camera Idle method");
}
@override
Widget build(BuildContext context) => ValueListenableBuilder<CameraPosition>(
valueListenable: currentCameraPosition,
builder: (BuildContext context, CameraPosition value, Widget? child) {
return GoogleMap(
initialCameraPosition: currentCameraPosition.value,
minMaxZoomPreference: const MinMaxZoomPreference(8, 19),
zoomControlsEnabled: false,
zoomGesturesEnabled: true,
scrollGesturesEnabled: true,
mapToolbarEnabled: false,
rotateGesturesEnabled: false,
tiltGesturesEnabled: false,
myLocationEnabled: true,
mapType: MapType.normal,
compassEnabled: false,
onMapCreated: _onMapCreated,
onCameraMove: onCameraMove,
onCameraIdle: onCameraIdle,
gestureRecognizers: Set()
..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer())),
markers: {
/// instead of using the centralized widget, use the Marker widget
Marker(
markerId: MarkerId(LatLng(currentCameraPosition.value.target.latitude, currentCameraPosition.value.target.longitude).toString()),
position: LatLng(currentCameraPosition.value.target.latitude, currentCameraPosition.value.target.longitude),
visible: true,
draggable: true,
/// you can change Marker color using the [BitmapDescriptor] blow.
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueCyan),
)
},
);
});
}
如果有什么不清楚的地方,请告诉我。