Adding multiple markers in ionic google map

允我心安 提交于 2019-12-02 03:13:05

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

    }

  });

});

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!