You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 08:15:03