Reputation: 296
I am trying to integrate google map in ionic project and got succeeded in displaying the google map on ionic page. But i want to display multiple markers on the this google map. tried different code but not getting this multiple markers thing. below is my code:
html:
<ion-content>
<div #mapElement class="map" >
</div>
.ts file:
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
declare var google;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit, AfterViewInit{
@ViewChild('mapElement', { static: true }) mapNativeElement: ElementRef;
constructor() {}
ngOnInit() {
}
ngAfterViewInit(): void {
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
}
i want to add diffent location markers on the map using latitude and longitude values similar to below reference image
please help me how can i do this. thank you
Upvotes: 0
Views: 2466
Reputation: 69
On one project i needed to do the same so i made this function. So you only need to put a for/foreach with the list you want to print and thats all.
Hope this help you.
private createMarker(position, label, element, icon) {
if (this.map != undefined) {
let marker = new google.maps.Marker({
position: position,
map: this.map,
icon: {
url: icon, // url
scaledSize: new google.maps.Size(40, 40), // scaled size
}
});
google.maps.event.addListener(marker, 'click', function () {
var infowindow = new google.maps.InfoWindow({
content: "content"
});
infowindow.open(this.map, marker);
});
} else {
console.log("map is undefined");
}
}
with loop you should be able to create the markers correctly with the function above:
this.markers.forEach(element => {
this.infomarkers.forEach(info => {
if (element.id == info.id) {
this.createMarker(element.position, element.label, info, element.icon);
}
});
});
Upvotes: 1
Reputation: 19
you can define some markers like this:
new google.maps.Marker({
map: map,
position: {lat: -34.397, lng: 150.644},
icon: {
url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
}
});
for more information see the tutorial here: https://medium.com/free-code-camp/how-to-change-javascript-google-map-marker-color-8a72131d1207
Upvotes: 0