Adding multiple markers in ionic google map

前端 未结 2 1563
借酒劲吻你
借酒劲吻你 2021-01-25 20:57

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.

相关标签:
2条回答
  • 2021-01-25 21:07

    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);
    
        }
    
      });
    
    });
    
    0 讨论(0)
  • 2021-01-25 21:12

    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

    0 讨论(0)
提交回复
热议问题