Nitneq
Nitneq

Reputation: 711

Multiple markers on Google Map with ionic 3

I'm trying to get several markers on a simple google map. I'm using Ionic 3 which use Angular 4.

I'm loading the map like this :

import { Component, ViewChild, ElementRef } from '@angular/core';
import { GoogleMaps } from '@ionic-native/google-maps';

declare var google;

export class SearchResultsPage extends BasePage {

@ViewChild('map') mapElement: ElementRef;
private map: any;

constructor(public navCtrl: NavController, 
            public navParams: NavParams, 
            private translateService: TranslateService,
            private googleMaps: GoogleMaps) {}


  ionViewDidLoad() {
   setTimeout(()=>{
      this.loadMap();
   }, 1000)
  }

  loadMap() {
    let latLng = new google.maps.LatLng(48.8509695, 2.3861870000000636);
    let latLng2 = new google.maps.LatLng(48.8504541, 2.3865487000000485);

    let mapOptions = {
        center: latLng,
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);    

    var marker = new google.maps.Marker({    
        position: latLng,
        map: this.map,
        title: 'Hello World!'
    },{
        position: latLng2,
        map: this.map,
        title: 'Check the World!'
    });
}

I found this (but doesn't work)

addMarkers(data, callback) {
    var markers = [{
        'position': {
            lat: 48.8513735,
            lng: 2.3861292
        },
        'icon': '#ff0000'
        }];
    function onMarkerAdded(marker) {
        console.log(marker);
        markers.push(marker);

        if (markers.length === data.length) {
            callback(markers);
        } else {
            console.log('in the else');
        }
    }

    data.forEach(function(markerOptions) {
        console.log('in foreach');
        this.map.addMarker(markerOptions, onMarkerAdded);
    });
}

The map is showing, but I don't succeed to add markers.
I tried to follow the official doc (v1 and v2) but it doesn't work. If someone has an idea, thanks by advance. I saw lot of people who try to do the same thing, but every codes are differents..

Upvotes: 2

Views: 9807

Answers (1)

eNVy
eNVy

Reputation: 487

loadMap() {
let latLng = new google.maps.LatLng(48.8513735, 2.3861292);

let mapOptions = {
  center: latLng,
  zoom: 15,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}

this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);    

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 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));
}
}

Upvotes: 6

Related Questions