0

I'm trying to create multiple markers in a Google Map with the AGM package. While I can create multiple markers hardcoding the latitude and longitude if I try to create them with data fetched from an API I can't make it work.

If I do a ngFor in a 'p' tag to show the latitude and longitude, it shows the data correctly but for some reason, it can't create the agm-markers. This is my code:

component.ts

gps_lats: Array<number>;
gps_longs: Array<number>;

constructor(private _api: ApiService) { }

ngOnInit() {
  this._api.machinesLocation().subscribe(res => {
    this.gps_lats = Object.values(res['data']).map(({ lat }) => lat);
    this.gps_longs = Object.values(res['data']).map(({ long }) => long);
  });
}

component.html

<div class="card ">
  <agm-map [latitude]="39.9" [longitude]="-0.16">
    <agm-marker *ngFor="let machine of gps_lats; index as i" [latitude]="gps_lats[i]" [longitude]="gps_longs[i]">
    </agm-marker>
  </agm-map>
</div>
4
  • hi @David plz provide json res of machinesLocation service Commented Jul 3, 2018 at 8:39
  • jsoneditoronline.org/?id=45a9246fb7d04f0a86b0f7286a5adcd2 Commented Jul 3, 2018 at 9:04
  • Ok I will create a Stackblitz on demo Commented Jul 3, 2018 at 9:05
  • Hi @David I Have post a Solution with Stackblitz demo. Commented Jul 3, 2018 at 10:18

2 Answers 2

2

You can try with this solution

I have create a demo on Stackblitz

Component.ts

 markers: Arary<any>=[];
 ngOnInit() {
        this._api.machinesLocation().subscribe((res: any) => {
            for(let data in res.data){
                this.markers.push({
                    lat: parseInt(res.data[data].lat),
                    long: parseInt(res.data[data].long)
                })
            }
        }
 }

Component.html

<agm-marker 
      *ngFor="let m of markers;"
      [latitude]="m.lat"
      [longitude]="m.long">
</agm-marker>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Krishna, this totally works. I had to fix a bit of the code as I'm using Angular 6 (just small things as fetching the data with res['data'] instead of res.data)', but now it works.
0

I'm using Angular 6

Up helped me a lot but there are still some things to fix. You can use for() as Krishna or .forEach() as I do, both should work.

  public markers: [] = [];

  lat: number = 51.247680;
  lng: number = 22.561402780001;
  zoom: number = 15;


this.httpService.getBusstops().subscribe( 
      // "key" is not even needed here, it's just leftover of(from?) my previous idea 
      res => { res['response'].forEach((stop, key) => {

         this.markers.push({

            lat: parseFloat(stop['latitude']), // In api I have one "step" less than OP so I'm not doing 'res['response'].stop'
            lng: parseFloat(stop['longitude']),
            label: stop['name']

         }) 

        })} console.log(this.markers);    

      )

Next, in html

<agm-map [(latitude)]="lat" [(longitude)]="lng" [(zoom)]="zoom" [disableDefaultUI]="true" [zoomControl]="true" [(fitBounds)]='viewport'>

    <agm-marker *ngFor="let marker of markers" [latitude]="marker['lat']" [longitude]="marker['lng']">
        <agm-info-window>
            <p>{{ marker.label }}</p>
        </agm-info-window>
    </agm-marker>

</agm-map>

It's important to put string indexes as x['y'].

I'm not sure what's the difference and wheather to use [latitude] or [(latitude)], I thought it has something to do with interfaces but even if I'm not using any, I see no difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.