Display only one marker when there should be several in Angular. Google map

I am using Angular Google Map from https://angular-maps.com . I wanted to show some markers using the ngFor directive for the agm-marker element. Although the DOM shows all the markers, the user interface shows only the first on the map.

Here is the 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 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

+4
source share
2 answers

I advise you to follow this link https://www.npmjs.com/package/@agm/js-marker-clusterer .

Then attach your for loop in this section of code:

<agm-marker [latitude]="51.673858" [longitude]="7.815982"></agm-marker>
Run code

] = "51.673858" [longitude] = "7.815982" >

+1

, @Dasikely

js-marker-clusterer , .

.

markers: any[];

.

private onData(data) {
    this.markers = [];
    for (const item of data) {
        this.markers.push({
            lat: item.latitude,
            lng: item.longitude
        });
    }
}

, , 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>
0

Source: https://habr.com/ru/post/1683584/


All Articles