Currently, I am fetching markers from the database and displaying them on a map using Angular Google Maps (AGM) by utilizing the <agm-marker>
tag.
In my code snippet below, you can see how I fetch and store the markers in an array named markers
in the file componentName.component.ts
:
ngOnInit(): void {
this.markerService.fetchMarkers()
this.markerService.markersSubject.subscribe(markers => {
markers.forEach(marker => {
this.markers.push({
lat: marker.latitude,
lng: marker.longitude,
title: marker.title,
story: marker.story,
path: marker.img_path
})
})
})
} //onInit
Below is the HTML code responsible for rendering these markers:
<agm-map
[latitude]="latitude"
[longitude]="longitude"
[zoom]="zoom"
[disableDefaultUI]="false"
[streetViewControl]="false"
[zoomControl]="false"
[mapTypeControl]="false"
[mapTypeId]="'terrain'"
>
<agm-marker id="display-markers"
*ngFor="let m of markers; let i = index"
(markerClick)="openModal('marker-content' + i)"
[latitude]="m.lat"
[longitude]="m.lng"
[markerDraggable]="m.draggable"
[iconUrl]="m.icon"
[visible]="true"
[id]="i"
</agm-marker>
</agm-map>
I now need to implement the functionality that allows users to clear all markers by pressing a button. This means setting the [visible]
attribute of each <agm-marker>
to False. How can I achieve this within the component.ts
file?
Are there alternative methods in Angular for removing all markers from the map?
I have searched extensively online but haven't found a solution yet. Any help is appreciated.
Thank you!