【问题标题】:How to show Markers on the map from firestore in flutter?如何在flutter中从firestore的地图上显示标记?
【发布时间】:2020-03-31 01:43:52
【问题描述】:

我正在尝试在 firestore 的地图上显示标记,并且我使用了谷歌地图颤振插件,但标记没有显示在地图上。我试图在模拟器和 Android 手机上运行该应用程序。 请帮忙。

这是代码:

  @override
void initState() {
super.initState();
populateClients();
Geolocator().getCurrentPosition().then((currloc) {
  setState(() {
    currentLocation = currloc;
  });
});
}

 populateClients() async {
var pos = await Geolocator().getCurrentPosition();
final coordinates = new coder.Coordinates(
    pos.latitude, pos.longitude);
var addresses = await coder.Geocoder.local.findAddressesFromCoordinates(
    coordinates);
var first = addresses.first;
initializeDateFormatting();
var format = DateFormat.d('ar');
var dateString = format.format(DateTime.now());
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;
Firestore.instance.collection('profiles')
      .where('City', isEqualTo: first.locality)
      .where('Days', arrayContains: dateString)
      .where('Uid' != uid)
      .where('role' == 'user')
    .getDocuments().then((docs) {
  if (docs.documents.isNotEmpty) {
    for (int i = 0; i < docs.documents.length; i++) {
      clients.add(docs.documents[i].data);
      initMarker(docs.documents[i].data,
          docs.documents[i].documentID);
    }
  }
});
}

void initMarker(client, markerRef) {
var markerIdVal = markerRef;
final MarkerId markerId = MarkerId(markerIdVal);


  final Marker marker = Marker(
    markerId: markerId,
    position: LatLng(double.parse(client['Location'].latitude),
        double.parse(client['Location'].longitude)),
    infoWindow: InfoWindow(title: "click here", snippet: "click to show the details"),
    icon: BitmapDescriptor.defaultMarker,
    onTap: () {
      showAlertDialog(client);
    }
);

setState(() {
// adding a new marker to map
markers[markerId] = marker;
});
}

代码有什么错误?

【问题讨论】:

    标签: google-maps flutter dart


    【解决方案1】:
    Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
    
    populateClients(){  
    Firestore.instance
    .collection('victims')
    .getDocuments()
    .then((docs){
      if(docs.documents.isNotEmpty){
        for(int i=0; i<docs.documents.length;i++){
        initMarker(docs.documents[i].data, docs.documents[i].documentID);
        }
      }
    
    } ); }
    
    void initMarker(request, requestId){
    
    var p=request['position'];
    var markerIdVal= requestId;
    final MarkerId markerId= MarkerId(markerIdVal);
    final Marker marker= Marker(
      markerId:  markerId,
      position: LatLng(p['geopoint'].latitude, p['geopoint'].longitude),
      infoWindow: InfoWindow (title: request['name'],snippet: request['address']),
    );
    setState(() {
      markers[markerId]=marker;
      
    }); }
    

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 2018-11-22
      • 1970-01-01
      • 2021-09-13
      • 2020-01-14
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多