如何在不影响应用程序性能的情况下向 Google 地图添加超过 450 个自定义标记?

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

如何在不影响应用程序性能的情况下向 Google 地图添加超过 450 个自定义标记?

标记会有在其后面添加边框、然后放置徽标等操作。

我尝试使用isolate,但应用程序变得很慢。

flutter dart google-maps
1个回答
0
投票
late GoogleMapController _controller;
late ClusterManager _manager;
final List<LatLng> _locations = List.generate(
 450,
 (index) => LatLng(
   37.7749 + index * 0.001,
   -122.4194 + index * 0.001,
  ),
);

 @override
 void initState() {
 super.initState();
 _manager = _initClusterManager();
}

 ClusterManager _initClusterManager() {
return ClusterManager<ClusterItem>(
  _locations.map((latLng) => ClusterItem(latLng)).toList(),
  _updateMarkers,
  markerBuilder: _markerBuilder,
  );
}

Future<Marker> Function(Cluster<ClusterItem>) get _markerBuilder =>
  (cluster) async {
    return Marker(
      markerId: MarkerId(cluster.getId()),
      position: cluster.location,
      icon: BitmapDescriptor.defaultMarkerWithHue(
        BitmapDescriptor.hueRed,
      ),
      infoWindow: InfoWindow(title: 'Cluster of ${cluster.count}'),
    );
  };

 void _updateMarkers(Set<Marker> markers) {
 setState(() {
   _controller.setMarkers(markers);
  });
 }

正文:谷歌地图( 初始相机位置:相机位置( 目标:LatLng(37.7749,-122.4194), 变焦:10, ), onMapCreated: (控制器) { _controller=控制器; _manager.setMapId(controller.mapId); }, 标记:_manager.markers, onCameraMove:_manager.onCameraMove, onCameraIdle:_manager.updateMap, ), );

© www.soinside.com 2019 - 2024. All rights reserved.