单击标记

时间:2019-01-02 04:25:11

标签: html google-maps google-maps-api-3 maps infobox

因此,我正在使用Google Maps api的信息框插件/附件,并且可以显示标记,但是当我单击它时什么也没有发生。我的代码主要来自该示例,但由于某些原因,它没有按预期弹出。我是否需要以某种方式导入它(因为当我声明一个新的InfoBox时,InfoBox没有突出显示)。我该怎么办?

信息框插件:https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js

我的jsfiddle:https://jsfiddle.net/kaidemarco/06wpx75j/49/

*此外,我还尝试使用显示您的代码进行格式化,但效果并不理想。

function initMap() {
  var Jays = {lat:  39.280126,lng: -74.574394};

 var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 14,
    center: Jays
  });

var marker = new google.maps.Marker({
            map: map,
            draggable: false,
            position: Jays,
            visible: true
        });

var boxText = document.createElement("div");
        boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
        boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";

var myOptions = {
             content: boxText
            ,disableAutoPan: false
            ,maxWidth: 0
            ,pixelOffset: new google.maps.Size(-140, 0)
            ,zIndex: null
            ,boxStyle: { 
              background: "url('tipbox.gif') no-repeat"
              ,opacity: 0.75
              ,width: "280px"
             }
            ,closeBoxMargin: "10px 2px 2px 2px"
            ,closeBoxURL: "https://www.google.com/intl/en_us/mapfiles/close.gif"
            ,infoBoxClearance: new google.maps.Size(1, 1)
            ,isHidden: false
            ,pane: "floatPane"
            ,enableEventPropagation: false
        };


google.maps.event.addListener(marker, "click", function (e) {
            ib.open(map, this);
      });
      var ib = new InfoBox(myOptions);
        ib.open(map, marker);

}

1 个答案:

答案 0 :(得分:0)

问题在于InfoBox依赖于Google Maps Javascript API。解决该问题的一种方法是按照正确的顺序同步加载API和InfoBox。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/googlemaps/v3-utility-library@master/infobox/src/infobox.js"></script>

在窗口加载时调用initMap函数:

google.maps.event.addDomListener(window, 'load', initMap);

proof of concept fiddle

screenshot of resulting map

function initMap() {
  var Jays = {
    lat: 39.280126,
    lng: -74.574394
  };

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 14,
    center: Jays
  });

  var marker = new google.maps.Marker({
    map: map,
    draggable: false,
    position: Jays,
    visible: true
  });

  var boxText = document.createElement("div");
  boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
  boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";

  var myOptions = {
    content: boxText,
    disableAutoPan: false,
    maxWidth: 0,
    pixelOffset: new google.maps.Size(-140, 0),
    zIndex: null,
    boxStyle: {
      background: "url('tipbox.gif') no-repeat",
      opacity: 0.75,
      width: "280px"
    },
    closeBoxMargin: "10px 2px 2px 2px",
    closeBoxURL: "https://www.google.com/intl/en_us/mapfiles/close.gif",
    infoBoxClearance: new google.maps.Size(1, 1),
    isHidden: false,
    pane: "floatPane",
    enableEventPropagation: false
  };


  google.maps.event.addListener(marker, "click", function(e) {
    ib.open(map, this);
  });
  var ib = new InfoBox(myOptions);
  ib.open(map, marker);

}
google.maps.event.addDomListener(window, 'load', initMap);
#map {
  height: 100%;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script src="https://cdn.jsdelivr.net/gh/googlemaps/v3-utility-library@master/infobox/src/infobox.js"></script>
<div id="map"></div>