其目标是利用内存中的信息,使用集群生成谷歌地图标记。目前,我正在下载LATLNG点从Firebase到本地内存。接下来,我们的目标是使用Google集群功能在地图上显示这些点的集合。为此,有一个名为clustering_google_maps 0.0.4+2的依赖项,它允许从本地数据库(SQLite)或本地内存访问数据。
开发人员建议使用数千个大型标记集,最好使用本地数据库(SQLite)。在我的例子中,我只有20-40个标记。有人能提供一个解决方案来解释如何使用本地内存的数据在Google上显示吗?
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();
}
},
),
);
}
}发布于 2019-04-17 18:01:54
内存技术要正常工作,您必须有一个LatLngAndGeohash对象的列表。LatLngAndGeohash是一个带有Location和Geo散列属性的简单对象,最后一个是自动生成的;您只需要点的位置。 对于此解决方案,必须使用ClusteringHelper的内存构造函数:
ClusteringHelper.forMemory(...);要测试这个特性,您可以克隆地图回购。然后使用以下代码修改splash.dart文件。从HomeScreen调用splash.dart时
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)https://stackoverflow.com/questions/55662174
复制相似问题