Flutter Firebase clustering_google_maps

问题描述 投票:0回答:1

目标是使用内存中信息的集群生成谷歌地图标记。目前,我正在从Firebase下载LATLNG点到本地内存。接下来的目标是使用Google地图群集功能在Flutter应用中的地图上显示这些点集合。为此,有一个名为clustering_google_maps 0.0.4+2的依赖项,它允许从本地数据库(SQLite)或本地内存访问数据。

开发人员建议使用数千个大型标记集,最好使用本地数据库(SQLite)。就我而言,我只有20到40个标记。有人可以帮助提供解决方案来解释如何使用本地内存中的数据显示在Google地图上吗?

Quick Example from the Repo

class HomeScreen extends StatefulWidget {
  final List<LatLngAndGeohash> list;

  HomeScreen({Key key, this.list}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  ClusteringHelper clusteringHelper;
  final CameraPosition initialCameraPosition =
      CameraPosition(target: LatLng(0.000000, 0.000000), zoom: 0.0);

  Set<Marker> markers = Set();

  void _onMapCreated(GoogleMapController mapController) async {
    print("onMapCreated");
    if (widget.list == null) {
      clusteringHelper.database = await AppDatabase.get().getDb();
    }
    clusteringHelper.updateMap();
  }

  updateMarkers(Set<Marker> markers) {
    setState(() {
      this.markers = markers;
    });
  }

  @override
  void initState() {
    if (widget.list != null) {
      initMemoryClustering();
    } else {
      initDatabaseClustering();
    }

    super.initState();
  }

  // For db solution
  initDatabaseClustering() {
    clusteringHelper = ClusteringHelper.forDB(
      dbGeohashColumn: FakePoint.dbGeohash,
      dbLatColumn: FakePoint.dbLat,
      dbLongColumn: FakePoint.dbLong,
      dbTable: FakePoint.tblFakePoints,
      updateMarkers: updateMarkers,
    );
  }

  // For memory solution
  initMemoryClustering() {
    clusteringHelper = ClusteringHelper.forMemory(
      list: widget.list,
      updateMarkers: updateMarkers,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Clustering Example"),
      ),
      body: GoogleMap(
        onMapCreated: _onMapCreated,
        initialCameraPosition: initialCameraPosition,
        markers: markers,
        onCameraMove: (newPosition) => clusteringHelper.onCameraMove(newPosition, forceUpdate: false),
        onCameraIdle: clusteringHelper.onMapIdle,
      ),
      floatingActionButton: FloatingActionButton(
        child:
            widget.list == null ? Icon(Icons.content_cut) : Icon(Icons.update),
        onPressed: () {
          if (widget.list == null) {
            clusteringHelper.whereClause = "WHERE ${FakePoint.dbLat} > 42.6";
            clusteringHelper.updateMap();
          } else {
            clusteringHelper.updateMap();
          }
        },
      ),
    );
  }
}
google-maps dart flutter
1个回答
0
投票

From the docs

内存技术要正常工作,您必须拥有LatLngAndGeohash对象列表。 LatLngAndGeohash是一个具有Location和Geohash属性的简单对象,最后一个是自动生成的;你只需要点的位置。

对于此解决方案,您必须使用ClusteringHelper的MEMORY构造函数:

ClusteringHelper.forMemory(...);

要测试此功能,您可以克隆giandifra/clustering_google_maps repo。然后使用以下代码修改splash.dart文件。从splash.dart调用HomeScreen时

  List<LatLngAndGeohash> markerList = new List<LatLngAndGeohash>();

  @override
  void initState() {
    super.initState();
    markerList.add(LatLngAndGeohash(LatLng(34.0522, -118.2437)));
    markerList.add(LatLngAndGeohash(LatLng(34.0522, -118.2647)));
    markerList.add(LatLngAndGeohash(LatLng(34.0522, -118.2467)));
    markerList.add(LatLngAndGeohash(LatLng(34.0522, -118.2487)));
    markerList.add(LatLngAndGeohash(LatLng(34.0522, -118.2707)));
  }

 @override
   Widget build(BuildContext context) {
     return Scaffold(
        ....
        HomeScreen(list: markerList)
© www.soinside.com 2019 - 2024. All rights reserved.