flutter - 如何阻止 GoogleMapController 的 animateCamera 方法连续调用,并且仅在按下当前位置按钮时平移?
问题描述
我有一个显示谷歌地图的应用程序,带有当前位置 FAB,当按下它时,将触发用户当前位置的动画。然而,一旦按下按钮并且地图现在位于用户的位置,就不可能平移谷歌地图,因为每次 Flutter 构建应用程序时,它都会围绕用户当前位置重新定位地图。我的_getCurrentLocation
函数是异步的,应该只在按下按钮时触发。
相关代码:
class _MapScreenState extends State<MapScreen> {
GoogleMapController _controller;
StreamSubscription _locationSubscription;
Marker marker; //adds the locator icon to mark current location in real time
Location _locationTracker = Location();
Circle circle; // adds a circle around the marker for visibility
static final _initialCameraPosition = CameraPosition(
target: LatLng(1.3521, 103.8198),
zoom: 11.5,
);
... etc ... //code to convert asset to an icon
updateMarker(LocationData _locationData, Uint8List imageData) async {
etc etc
}
//function to get the current location using geocoder api
void _getCurrentLocation() async {
print("executing _getCurrentLocation ");
try {
Uint8List imageData = await getMarker();
var currentLocation = await _locationTracker.getLocation();
updateMarker(currentLocation, imageData);
if (_locationSubscription != null) {
_locationSubscription.cancel();
}
_locationSubscription =
_locationTracker.onLocationChanged.listen((LocationData newLocalData) {
if (_controller != null) {
_controller
.animateCamera(CameraUpdate.newCameraPosition(new CameraPosition(
bearing: 192,
target: LatLng(newLocalData.latitude, newLocalData.longitude),
tilt: 0,
zoom: 14.00,
)));
updateMarker(newLocalData, imageData);
}
});
} on PlatformException catch (error) {
if (error.code == 'PERMISSION DENIED') {
debugPrint("Permission Denied");
}
}
}
@override
Widget build(BuildContext context) {
print("build ");
return Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
myLocationButtonEnabled: false,
zoomControlsEnabled: false,
initialCameraPosition: _initialCameraPosition,
markers: Set.of((marker != null) ? [marker] : []),
circles: Set.of((circle != null) ? [circle] : []),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
},
),
Positioned(
top: 70.0,
right: 15.0,
left: 15.0,
child: Column(children: <Widget>[
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(30),
),
child: TextField(
decoration: InputDecoration(
labelText: "Going to?",
contentPadding:
EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
prefixIcon: Icon(Icons.search),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
)),
)
])),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _getCurrentLocation,
child: Icon(Icons.location_pin),
backgroundColor: Colors.cyan,
));}}
我正在使用onLocationChanged更新 GoogleMapController的_getCurrentLocation
函数。整个函数只有在floatingActionButton被按下的时候才会调用,但实际上好像每次flutter构建程序时都会触发?_controller.animateCamera
解决方案
推荐阅读
- angular - 提交重定向返回错误后 Ionic 4 重置表单不清楚
- angular - Ionic 3“遇到未定义的提供者!通常这意味着你有一个循环依赖”可能是由使用 'barrel' index.ts 文件引起的
- python - 使用神经网络 Python 进行多步时间序列预测会给出不正确的结果
- python - 在 Python 中使用 Keras 自定义奖励损失函数
- html - 努力在路径 SVG 对象中添加文本
- python - 使用 Scrapy 同时从当前链接和嵌套链接中抓取数据
- c# - 重复子串 N 次
- android - Google Play 服务登录问题 - SHA1 指纹问题
- php - 使用 array_search 函数时数组中的第一个值重复
- dask - 如何在与提交它的机器不同的机器上获得 Dask 计算的结果?