1

I am trying to display 2 markers on the google maps. But I am not aware of how to do it. I am able to see only one marker on the map.

Here is my map.component.html file:

<agm-map [latitude]="lat" [longitude]="lng">
<agm-marker [latitude]="lat" [longitude]="lng">
</agm-marker>
</agm-map>

Here is my map.component.ts file:

import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-marker',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MarkerComponent implements OnInit {
constructor() { }
jsonObject = [
{
deviceid: 123,
source: [10.8505, 76.2711],
destination: [34.083656,  74.797371],
location: [22.5726, 88.3639],
speed: 45,
driver: 'ABC',
vehiclenumber: 'KA01R45',
vehiclebrand: 'Honda',
vehiclemodel: 'CRDI',
status: 'danger'
},
{
deviceid: 345,
source: [13.0827, 80.2707],
destination: [19.0760, 72.8777],
location: [19.7515,  75.7139 ],
speed: 60,
driver: 'DEF',
vehiclenumber: 'KA01L45',
vehiclebrand: 'Hyundai',
vehiclemodel: 'CRDI',
status: 'idle'
}
]
lat: number = 39.817490;
lng: number = -0.231035;
ngOnInit() { 
this.getMarker();
}

getMarker() {
this.lat = { lat: this.jsonObject.location[0], lng: this.jsonObject.location[1]}
this.lng = { lat: this.jsonObject.location[0], lng: this.jsonObject.location[1]}
}
}

I am iterating through jsonObject to get the location of two different objects. I want to display the latitude and longitude of location given in the json.

1 Answer 1

2

Use *NgFor For Multiple Marker

 <agm-marker 
      *ngFor="let m of markers; let i = index"
      (markerClick)="clickedMarker(m.label, i)"
      [latitude]="m.lat"
      [longitude]="m.lng"
      [label]="m.label"
      [markerDraggable]="m.draggable"
      (dragEnd)="markerDragEnd(m, $event)">

Example:https://stackblitz.com/edit/angular-google-maps-demo-za7bmp

Sign up to request clarification or add additional context in comments.

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.