如何在 google 地图中添加来自 firestore 的标记?
How to add markers from the firestore in the google maps?
我试图从 Internet 构建一个代码,它应该在 goole 地图中绘制两个标记。困难在于坐标来自云 Firestore。我不知道为什么,但我看不到标记。我按照教程中的人告诉我的方式进行操作。
这是我的 firestore 的截图:
这是我试过的代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class maps2 extends StatefulWidget {
@override
_maps2State createState() => _maps2State();
}
class _maps2State extends State<maps2> {
GoogleMapController controller;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
void initMarker(specify, specifyId) async {
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position:
LatLng(specify['location'].latitude, specify['location'].longitude),
infoWindow: InfoWindow(title: 'Shop', snippet: specify['name']),
);
setState(() {
markers[markerId] = marker;
});
}
getMarkerData() async {
Firestore.instance.collection('data').getDocuments().then((myMockDoc) {
if (myMockDoc.documents.isNotEmpty) {
for (int i = 0; i < myMockDoc.documents.length; i++) {
initMarker(
myMockDoc.documents[i].data, myMockDoc.documents[i].documentID);
}
}
});
}
@override
void initState() {
getMarkerData();
super.initState();
}
@override
Widget build(BuildContext context) {
Set<Marker> getMarker() {
return <Marker>[
Marker(
markerId: MarkerId('Shop'),
position: LatLng(21.1458, 79.0882),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: 'Home'))
].toSet();
}
return Scaffold(
body: GoogleMap(
markers: Set<Marker>.of(markers.values),
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(21.1458, 79.0882), zoom: 12.0),
onMapCreated: (GoogleMapController controller) {
controller = controller;
},
),
);
}
}
您需要将代码更改为:
initMarker(
myMockDoc.documents[i].data(), myMockDoc.documents[i].documentID);
}
我试图从 Internet 构建一个代码,它应该在 goole 地图中绘制两个标记。困难在于坐标来自云 Firestore。我不知道为什么,但我看不到标记。我按照教程中的人告诉我的方式进行操作。
这是我的 firestore 的截图:
这是我试过的代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class maps2 extends StatefulWidget {
@override
_maps2State createState() => _maps2State();
}
class _maps2State extends State<maps2> {
GoogleMapController controller;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
void initMarker(specify, specifyId) async {
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position:
LatLng(specify['location'].latitude, specify['location'].longitude),
infoWindow: InfoWindow(title: 'Shop', snippet: specify['name']),
);
setState(() {
markers[markerId] = marker;
});
}
getMarkerData() async {
Firestore.instance.collection('data').getDocuments().then((myMockDoc) {
if (myMockDoc.documents.isNotEmpty) {
for (int i = 0; i < myMockDoc.documents.length; i++) {
initMarker(
myMockDoc.documents[i].data, myMockDoc.documents[i].documentID);
}
}
});
}
@override
void initState() {
getMarkerData();
super.initState();
}
@override
Widget build(BuildContext context) {
Set<Marker> getMarker() {
return <Marker>[
Marker(
markerId: MarkerId('Shop'),
position: LatLng(21.1458, 79.0882),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: 'Home'))
].toSet();
}
return Scaffold(
body: GoogleMap(
markers: Set<Marker>.of(markers.values),
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(21.1458, 79.0882), zoom: 12.0),
onMapCreated: (GoogleMapController controller) {
controller = controller;
},
),
);
}
}
您需要将代码更改为:
initMarker(
myMockDoc.documents[i].data(), myMockDoc.documents[i].documentID);
}