I have tried to build a code from the internet which should draw two markers in goole maps. The difficulty is that the coordinates come from a cloud firestore. I don't know why, but I can`t see the markers. I did it the same way the people in the tutorial told me.
This is a screenshot of my firestore:
And this is the code I tried:
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;
},
),
);
}
}
You need to change the code to:
initMarker(
myMockDoc.documents[i].data(), myMockDoc.documents[i].documentID);
}