簡體   English   中英

Google地圖疊加層可點擊

[英]Google maps overlay clickable

我是javascript新手。 在地圖中,當單擊疊加層時,我正在嘗試制作它,以便調用可以訪問單擊的疊加層的函數。

這是我的代碼,在控制台中沒有出現任何錯誤。

<div style='width: 800px;'>
  <div id="multi_overlays" style='width: 1000px; height: 600px;'></div>
</div>

<script type="text/javascript">
    handler = Gmaps.build('Google');

    handler.buildMap({ internal: {id: 'multi_overlays'}}, function(){

      var circles = handler.addCircles(
        [{ lat: 41.991873, lng: -70.652213, radius: 2000 }],
        { strokeColor: '#FF0001'}
      );

        var polylines = handler.addPolylines(
          [
          [
          ]
        ],
        { strokeColor: '#FF0000'}
      );


        var polygons = handler.addPolygons(
          [
            //ccb_nw
              [{lat:41.83,lng:-70.44},{lat:41.87,lng:-70.28},{lat:41.77,lng:-70.49},{lat:41.75,lng:-70.19}],
            //ccb_se
              [{lat: 41.984198, lng: -70.319776}, {lat: 41.970926, lng: -70.110349},{lat: 41.825776, lng: -70.164594},{lat: 41.803259, lng: -70.222959}],
            //ccb_sw
              [{lat: 41.887260, lng: -72.529131}, {lat: 41.920867, lng: -70.335363},{lat: 41.794781, lng: -70.283835},{lat: 41.783006, lng: -70.498755}],
          ],
              { strokeColor: '#FF0000'}
        );


      handler.bounds.extendWith(polylines);
      handler.bounds.extendWith(polygons);
      handler.bounds.extendWith(circles);
      handler.fitMapToBounds();  

      markers = handler.addMarkers([{"lat":42.30010009999999,"lng":-70.2994764,"infowindow":"Bass bite is picking up"},{"lat":41.6820897,"lng":-69.95976639999999,"infowindow":"Tuna non existent"},{"lat":42.0417525,"lng":-70.6722767,"infowindow":"good trip"},{"lat":42.072454,"lng":-70.206835,"infowindow":"On fire"}]);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();


    });
</script>

我嘗試使用Google Maps addListener事件來監聽點擊:

handler.addListener('click', function(event){
  alert('the map was clicked');
});

上面的代碼不起作用。 出現疊加層,但是單擊它們時沒有任何反應。 我認為這可能與窗格有關:

https://developers.google.com/maps/documentation/javascript/reference?csw=1#MapPanes

但是每次我嘗試添加overlayMouseTarget時,它都會告訴我該對象不存在。 我很失落。 如何使疊加層可點擊?

您將需要使用為所使用的標記/圓圈設置的變量創建一個偵聽器。 這應該可以幫助您:

google.maps.event.addListener(<variable>, 'click', function (event) {
      handleClick(event); // Your function here
});

我在這里有一個更大的例子: https : //snippetbox.xyz/9eb54a2a1f52dc1f5d41/

暫無
暫無

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

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