我试图通过检索地理点在firestore的谷歌地图上显示标记,但它没有显示标记,而是只在相机位置显示一个。下面给出了我的代码。我找不到我在哪里犯的错。这些方法没有从firestore中检索任何值。
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class heatmap extends StatefulWidget {
@override
_heatmapState createState() => _heatmapState();
}
class _heatmapState extends State<heatmap> {
var clients=[];
GoogleMapController mapController;
List<Marker> allMarkers=[];
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future<void> initState() {
setState(() {
clients=[];
FirebaseFirestore.instance.collection('markers').get().then((docs) {
if(docs.docs.isNotEmpty){
for(int i=0;i<docs.docs.length;i++){
initMarker(docs.docs[i].data());
}
}
});
});
}
initMarker(clients){
allMarkers.add( Marker(
markerId: MarkerId('my'),
position: LatLng(clients['coords'].latitude,clients['coords'].longitude),
)
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text('Red Flag areas',
style: TextStyle(
color: Colors.white,
letterSpacing: 2.0,
fontSize: 28.0,
)),
centerTitle: true,
backgroundColor: Colors.amberAccent,
),
body: GoogleMap(
onMapCreated: onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(23.8103, 90.4125),
zoom: 11.0
),
myLocationEnabled: true,
markers: Set.from(allMarkers),
)
);
}
void onMapCreated(controller){
setState(() {
mapController=controller;
});
}
}
这是数据库
按如下方式移动initMarker()
方法中的setState
调用:
initMarker(clients) {
setState(() => allMarkers.add(Marker(...)) );
}