【问题标题】:How do I add a new button to the infowindow on the marker?如何在标记上的信息窗口中添加新按钮?
【发布时间】:2018-04-26 11:04:55
【问题描述】:

这个JS代码

function initMap() {
  var uluru = {lat: -25.363, lng: 131.044};
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: uluru
  });

  var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
      '<div id="bodyContent">'+
      '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
      'sandstone rock formation in the southern part of the '+
      'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '
      </p>'+
      '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
      'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
      '(last visited June 22, 2009).</p>'+
      '</div>'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  var marker = new google.maps.Marker({
    position: uluru,
    map: map,
    title: 'Uluru (Ayers Rock)'
  });
  marker.addListener('click', function() {
    infowindow.open(map, marker);
  });
}

我想在信息窗口中添加一个名为“uluru”的新按钮。按钮应该像照片一样在关闭键旁边。你能帮我吗??

【问题讨论】:

    标签: javascript angularjs google-maps


    【解决方案1】:

    您可以通过使用contentString 设置内容来创建一个按钮,并将其定位到您想要的位置,如下面的演示。

    function initMap() {
      var uluru = {
        lat: -25.363,
        lng: 131.044
      };
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: uluru
      });
    
      var contentString = '<div id="content">' + '<button type="button" class="custom_button">!!!</button>' +
        '<div id="siteNotice"></div>' +
        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>' +
        '<div id="bodyContent">' +
        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
        'sandstone rock formation in the southern part of the ' +
        'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi)</p>' +
        `<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">` +
        'https://en.wikipedia.org/w/index.php?title=Uluru</a> ' +
        '(last visited June 22, 2009).</p>' +
        '</div>' +
        '</div>';
    
      var infowindow = new google.maps.InfoWindow({
        content: contentString
      });
    
      var marker = new google.maps.Marker({
        position: uluru,
        map: map,
        title: 'Uluru (Ayers Rock)'
      });
      marker.addListener('click', function() {
        infowindow.open(map, marker);
      });
    }
    
    initMap()
    #map {
      height: 90vh;
    }
    
    .custom_button {
      position: absolute;
      top: 0px;
      right: 20px;
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多