【发布时间】:2021-10-05 06:34:05
【问题描述】:
我正在使用 Flutter 地图包 (https://pub.dev/packages/flutter_map),因为我不想在这个项目中使用 Googlemaps。问题是我从未使用过它并且文档很差,所以我无法将相机移动到点击标记:
mapController.move(LatLng(latitude, longitude), 10.0);
当然还有像上面这样声明地图控制器:
MapController mapController = MapController();
基本上,移动功能应该可以在Tap上运行,但由于某种原因它不起作用,它给了我一些奇怪的运行时错误,你知道我做错了什么吗?
代码如下:
Widget loadMap() {
...Streambuilder...
builder: (context, snapshot) {
if (!snapshot.hasData) return Text('Loading maps... Please wait');
for (int i = 0; i < snapshot.data.docs.length; i++) {
allMarkers.add(Marker(
width: 45.0,
height: 45.0,
point: LatLng(
snapshot.data.docs[i]['location'].latitude,
snapshot.data.docs[i]['location'].longitude,
),
builder: (context) => Column(
children: [
Expanded(
child: Container(
child: IconButton(
icon: Icon(Icons
.location_on),
color: _markerColor,
iconSize: 45.0,
onPressed: () {
//this doesn't work here
mapController.move(LatLng(latitude, longitude), 10.0);
print(snapshot.data.docs[i]['eventName']);
showModalBottomSheet(
isDismissible:
false,
context: context,
builder: (builder) {
if (snapshot.data.docs[i]['description'].length >
0) {
//not the most elegant solution
}
});
//this returns error when marker is clicked, and doesn't move the camera to the marker
mapController.move(
LatLng(
snapshot.data.docs[i]['location'].latitude,
snapshot.data.docs[i]['location'].longitude,
),
10.0);
},
),
),
),
],
),
));
}
return FlutterMap(
//flutter map displayed with Mapbox
);
},
}
在 initState 中添加了地图控制器,但仍然报错:
void initState() {
super.initState();
mapController = MapController();
}
-错误:
The following LateError was thrown while handling a gesture:
LateInitializationError: Field '_state@244051772' has not been initialized.
When the exception was thrown, this was the stack
#0 MapControllerImpl._state (package:flutter_map/src/map/map.dart)
package:flutter_map/…/map/map.dart:1
#1 MapControllerImpl.move
package:flutter_map/…/map/map.dart:41
#2 _MapScreenState.loadMap.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:directr/pages/map_screen.dart:67
【问题讨论】:
-
你应该在 Github 问题上发布这个。
-
我不认为这是一个问题,只是我不确定代码是如何工作的,因为文档很差。
-
你能提供完整的、最小可复制的例子吗
-
我添加了整页代码。它从 Firestore 获取数据,但例如它不需要,我只希望相机转到单击的标记。
-
让我试一试,我会尽快回复您。