首页 > 解决方案 > 在 Google 地图中设置标签

问题描述

我用 x 个不同的别针制作了一个谷歌地图。当我将鼠标悬停在图钉上时,我可以看到 html 代码:

['Company a<br>\
Address x, Dk-2750 <br>\
<a href="#">Show way</a>

我尝试设置标题和标签,因此我可以选择将鼠标悬停在图钉上时的文本内容。有谁知道当我将鼠标悬停在图钉上时如何在图钉上设置标签?

<script>
  function initMap() {
    var center = {lat: 56.2639, lng: 9.5018};
    var locations = [
      ['Company a<br>\
      Address x, Dk-2750 <br>\
     <a href="#" alt>Show way</a>',   55.730097, 12.375905],

      ['Company b<br>\
      Address y, Dk-6600<br>\
     <a href="#">Show way</a>', 55.769726, 12.497519],

      ['Company c<br>\
      Address z, Dk-6950<br>\
      <a href="#">Show way</a>', 56.110692, 8.322713],
    ];
  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 7,
      center: center
    });
  var infowindow =  new google.maps.InfoWindow({});
  var marker, count;
  for (count = 0; count < locations.length; count++) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[count][1], locations[count][2]),
        map: map,
        title: locations[count][0]
      });
  google.maps.event.addListener(marker, 'click', (function (marker, count) {
        return function () {
          infowindow.setContent(locations[count][0]);
          infowindow.open(map, marker);
        }
      })(marker, count));
    }
  }
</script>

在此处输入图像描述

标签: javascriptgoogle-maps

解决方案


我想您应该在 google.maps.event 下方再添加一个事件列表器...您可以尝试google.maps.events.addListener(marker,'hover',function(..){...})或类似的方法。就像我难过一样,我只是在猜测:)


推荐阅读