无法从Firebase显示Google Maps标记

时间:2019-06-12 10:45:00

标签: google-maps flutter dart

查询Firebase后,位置信息存储为“标记”集合下的地理位置(纬度和经度)后,GoogleMaps标记未显示。

我一直关注Flutter GoogleMaps插件上的更改日志,其中将标记作为其自己的小部件进行创建,但没有成功。我使用Firebase.instance连接到数据库,并通过“标记”进行查询。 for循环尝试返回相关信息。 populateClients和initMarkers是使用GoogleMaps最近标记更新中描述的过程创建的将标记添加到地图的方法。

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

 class FirstScreen extends StatefulWidget {
  const FirstScreen({Key key}) : super(key: key);

  @override
  State<FirstScreen> createState() => _FirstScreen();
 }

    class _FirstScreen extends State<FirstScreen> {
   GoogleMapController mapController;

  var currentLocation;

  var clients = [];

   Map<MarkerId, Marker> markers = <MarkerId, Marker>{};

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


  populateClients() {

    Firestore.instance.collection('markers').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);

       //new marker

      final Marker marker = Marker(
        position: LatLng(client['location'].latitude,   
 client['location'].longitude),
        markerId: markerId,
      );

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

  @override
    Widget build(BuildContext context) {
    return currentLocation == null ? Container(
  alignment: Alignment.center,
  child: Center(
    child: CircularProgressIndicator(),
  ),
):
Stack(
  children: <Widget>[
    GoogleMap(
      initialCameraPosition:
      CameraPosition(target: LatLng(currentLocation.latitude,
          currentLocation.longitude), zoom: 17),
      onMapCreated: _onMapCreated,
      myLocationEnabled: true,
      mapType: MapType.normal,
      markers: Set<Marker>.of(markers.values),
    ),
  ],
);
  }



  void _onMapCreated(GoogleMapController controller) {
   setState(() {
      mapController = controller;
    });
  }


 }

标记将显示在Firebase中存储的地理位置点上。

0 个答案:

没有答案