【问题标题】:Google map custom marker with pop-up (Info Windows) on click点击时带有弹出窗口(信息窗口)的谷歌地图自定义标记
【发布时间】:2018-04-03 18:45:21
【问题描述】:

我已经使用自定义标记自定义了 Google 地图。我需要将 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/

【问题讨论】:

    标签: javascript jquery html css google-maps


    【解决方案1】:

    您想将什么内容作为 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/

    祝你有美好的一天。

    【讨论】:

    • 太棒了..!我怎样才能把一个链接到弹出窗口?意思是,和那个信息窗口一样?例如:&lt;a href="#link"&gt;Link&lt;/a&gt; 可以自定义弹窗吗?
    • 当然是这样的:jsfiddle.net/uqLxnyca/1你可以在谷歌文档developers.google.com/maps/documentation/javascript/examples/…找到可能的信息
    • 还有一件事..所有标记的内容似乎相同..是否可以添加单独的内容?
    • 喜欢这个? jsfiddle.net/pL0n6xu1我设置了点击标记时信息窗口的内容
    • 不是那样,我需要为每个标记显示一些自定义内容.. 不是LatLng 位置
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2011-08-21
    相关资源
    最近更新 更多