Flutter使用google_maps_cluster_manager实现地图聚合时place.dart相关问题咨询
import 'place.dart' 对应的是需要你自行创建的自定义点位模型文件,google_maps_cluster_manager 包要求参与聚合的点位必须实现 ClusterItem 接口,你可以参考以下代码编写:
完整 place.dart 代码
import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; class Place extends ClusterItem { // 下方为自定义业务属性,可根据实际需求增减 final String placeId; final String placeName; final String? description; // ClusterItem 接口强制要求实现的属性,不可删除 @override final LatLng latLng; // 构造函数 Place({ required this.placeId, required this.placeName, this.description, required this.latLng, }); // 可选工具方法:用于修改属性生成新的Place实例,非必须 Place copyWith({ String? placeId, String? placeName, String? description, LatLng? latLng, }) { return Place( placeId: placeId ?? this.placeId, placeName: placeName ?? this.placeName, description: description ?? this.description, latLng: latLng ?? this.latLng, ); } }
使用注意事项
- 类名可以自定义,不一定非要叫
Place,只要实现ClusterItem接口、保留latLng属性即可 - 点位初始化示例:
List<Place> clusterItems = [ Place(placeId: "p1", placeName: "中关村", latLng: const LatLng(39.9847, 116.3046)), Place(placeId: "p2", placeName: "国贸", latLng: const LatLng(39.9103, 116.4672)), ];
- 确认
pubspec.yaml中已经正确添加google_maps_flutter和google_maps_cluster_manager依赖,避免导入报错
内容的提问来源于stack exchange,提问作者Datapalace
相关产品推荐
相关产品推荐

