簡體   English   中英

點擊時彈出的Google地圖自定義標記(信息窗口)

[英]Google map custom marker with pop-up (Info Windows) on click

我已經使用自定義標記自定義了Google Map。 我需要將Info Windows集成到每個標記中。

來自以下位置的自定義標記代碼: https : //developers.google.com/maps/documentation/javascript/custom-markers

嘗試從以下位置集成信息窗口https//developers.google.com/maps/documentation/javascript/examples/infowindow-simple

這是我發現的一個相關問題(但並不是我想要的): https : //stackoverflow.com/a/3059129/6191987

我的代碼如下:

  html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } 
 <div id="map"></div> <script> var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 17, center: new google.maps.LatLng(40.712696, -74.005019), mapTypeId: 'roadmap' }); var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; var icons = { parking: { icon: iconBase + 'parking_lot_maps.png' }, library: { icon: iconBase + 'library_maps.png' }, info: { icon: iconBase + 'info-i_maps.png' } }; var features = [{ position: new google.maps.LatLng(40.712696, -74.005019), type: 'parking' }, { position: new google.maps.LatLng(40.712753, -74.006081), type: 'parking' }, { position: new google.maps.LatLng(40.713664, -74.007819), type: 'library' }]; // Create markers. features.forEach(function(feature) { var marker = new google.maps.Marker({ position: feature.position, icon: icons[feature.type].icon, map: map }); }); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap"> </script> 

還添加了JSFiddle: https ://jsfiddle.net/vishnuprasadps/7g33j2kz/

您想把什么作為infoWindow的內容?

但這似乎可以解決問題:

<div id="map"></div>

<script>
  var map;

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      center: new google.maps.LatLng(40.712696, -74.005019),
      mapTypeId: 'roadmap'
    });

    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    var icons = {
      parking: {
        icon: iconBase + 'parking_lot_maps.png'
      },
      library: {
        icon: iconBase + 'library_maps.png'
      },
      info: {
        icon: iconBase + 'info-i_maps.png'
      }
    };

    var features = [{
      position: new google.maps.LatLng(40.712696, -74.005019),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.712753, -74.006081),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.713664, -74.007819),
      type: 'library'
    }];

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

    // Create markers.
    features.forEach(function(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,
        map: map
      });
      marker.addListener('click', function() {
        infowindow.open(map, marker);
      });
    });





  }

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>

https://jsfiddle.net/uqLxnyca/

祝你有美好的一天。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM