Reputation: 1132
I am using Angular Google Map from https://angular-maps.com . I wanted to show multiple markers using ngFor directive on agm-marker element. Though DOM shows all of the markers are present, UI shows only the first one on the map.
Here is HTML part:
<agm-map [latitude]="lat" [longitude]="long" [zoom] = 16>
<agm-marker *ngFor="let m of latlongs; let i = index"
[latitude]="m.lat"
[longitude]="m.long"
[markerDraggable]="m.draggable"></agm-marker>
</agm-map>
Typescript code is here:
this.service.getData().subscribe(data => {
let json: any[] = [];
json["data"] = data.json().map(it => {
it["category"] = "location";
it["gallery"] = ["../assets/img/area.png"];
return it;
});
for(let i = 0; i < json["data"].length; i++){
this.mapInfos.push({lat: +json["data"][i].lat, long: +json["data"][i].long});
}
this.latlongs = this.mapInfos;
});
I am using angular 4 for my project
Upvotes: 4
Views: 2134
Reputation: 5565
An addition to the answer provided by @Dasikely
I used the same js-marker-clusterer and implemented multiple markers using the solution below.
In the component, I declared an array of markers.
markers: any[];
Updated this markers with the data from the service.
private onData(data) {
this.markers = [];
for (const item of data) {
this.markers.push({
lat: item.latitude,
lng: item.longitude
});
}
}
And finally used them in the view using ngFor
.
<agm-map [latitude]="initialLatitude" [longitude]="initialLongitude" [zoom]="zoom">
<agm-marker-cluster *ngFor="let marker of markers" imagePath="https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m">
<agm-marker [latitude]="marker.lat" [longitude]="marker.lng">
</agm-marker>
</agm-marker-cluster>
</agm-map>
Upvotes: 0
Reputation: 625
I advise you to follow this link https://www.npmjs.com/package/@agm/js-marker-clusterer.
Then attach your for loop in this code section :
<agm-marker [latitude]="51.673858" [longitude]="7.815982"></agm-marker>
latitude]="51.673858" [longitude]="7.815982">
Upvotes: 1