Display infowindow by default and different markers in Google Maps

前端 未结 2 1780
轻奢々
轻奢々 2020-12-07 03:40

My HTML:

My JQuery:

var infowindow = null;         


        
相关标签:
2条回答
  • 2020-12-07 03:59
    1. to use custom icons, for a simple icon, just add an icon URL to your sites array, use it in your marker definition.

              icon: sites[5]
      
    2. keep references to the markers:

      // in the global scope
      var gmarkers = [];
      
      
      
      for (var i = 0; i < markers.length; i++) {
          var sites = markers[i];
          var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
          var marker = new google.maps.Marker({
              position: siteLatLng,
              map: map,
              title: sites[0],
              zIndex: sites[3],
              html: sites[4],
              icon: sites[5]
          });
          gmarkers.push(marker);
          var contentString = "Some content";
      
          google.maps.event.addListener(marker, "click", function () {
              //alert(this.html);
              infowindow.setContent(this.html);
              infowindow.open(map, this);
          });
      
    3. Then to click on the 1st marker (which will open the infowindow):

      google.maps.event.trigger(gmarkers[0], "click");

    working example

    0 讨论(0)
  • 2020-12-07 04:10

    To show site[0] infobubble on page load by default.

    infowindow.open(map, site[0]);
    

    To have different marker for each on the map. Assuming you mean different markers images, you could either set them while creating markers or simply call site[i].setIcon('newImage.png'), later

    ref:Google Maps API v3: How do I dynamically change the marker icon?

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