确保已经正确引入了Google Maps API和Marker Clusterer库。
检查是否已正确安装聚类器组件。使用以下命令安装:npm install @google/markerclustererplus --save。
在需要使用聚类器组件的组件中引入聚类器库和组件:
import { MarkerClusterer } from '@google/markerclustererplus';
@Component({ selector: 'app-map', templateUrl: './map.component.html', styleUrls: ['./map.component.css'] }) export class MapComponent implements OnInit { // ...
ngOnInit(): void { // ...
const options = { imagePath: '/assets/images/m' }; // 图标路径
const markerCluster = new MarkerClusterer(this.map, markers, options);
} }
"scripts": [ // ... "node_modules/@google/markerclustererplus/dist/markerclusterer.min.js" ],
import { AfterViewInit } from '@angular/core';
export class MapComponent implements OnInit, AfterViewInit { // ...
ngAfterViewInit(): void { const options = { imagePath: '/assets/images/m' }; const markerCluster = new MarkerClusterer(this.map, [], options); // 将markers列表为空 markerCluster.addListener('clusteringend', () => { console.log('Markers clustered!'); }); } }
通过以上方法,可以解决Angular地图标记聚类器组件不起作用的问题。