亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在地圖上的google api中添加一個按鈕

如何在地圖上的google api中添加一個按鈕

胡說叔叔 2022-10-27 16:35:52
我想在谷歌地圖上的某個位置添加一個按鈕,例如緯度:62.323907 和經度:-150.109291。我可以添加按鈕,但無法單擊它。有沒有辦法在可以點擊的地圖上添加按鈕?在 CustomImageOverlay.prototype.onAdd() 中,我在此處創建 div 并在此處創建按鈕。我為 div 和按鈕提供 style.cursor = "pointer"。但是,該按鈕顯示在地圖上,位置正確,但無法單擊。光標也保持為可拖動的,它只允許我拖動而不是單擊。這是我的代碼:var map;google.maps.event.addDomListener(window, 'load', GoogleMap);var chicago = { lat: 41.85, lng: -87.65 };CustomImageOverlay.prototype = new google.maps.OverlayView();function GoogleMap() {  var pos =   { lat: 62.323907, lng: -150.109291}  map = new google.maps.Map(document.getElementById('googleMap'), {    center: pos,    zoom: 8  });  overlay = new CustomImageOverlay(map, pos);}function CustomImageOverlay(map, position) {  this.map_ = map;  this.position_ = position;  this.div_ = null;  this.setMap(map);}CustomImageOverlay.prototype.onAdd = function() {  var div = document.createElement('div');  div.style.borderStyle = 'solid';  div.style.borderWidth = '0px';  div.style.position = 'absolute';  div.style.cursor = "pointer";  // Create Button  var button = document.createElement("button");  button.innerHTML = "Click Here!";  button.style.position = 'absolute';  button.style.cursor = "pointer";  div.appendChild(button);  button.addEventListener("click", function() {    map.setCenter(chicago);  });  this.div_ = div;  // Add the element to the "overlayLayer" pane.  var panes = this.getPanes();  panes.overlayLayer.appendChild(div);};CustomImageOverlay.prototype.draw = function() {  var overlayProjection = this.getProjection();  var div = this.div_;    var userLatLng = new google.maps.LatLng(this.position_);  var posToPix = overlayProjection.fromLatLngToDivPixel(userLatLng);   div.style.left = posToPix.x + 'px';   div.style.top = posToPix.y + 'px';   div.style.width = '100px';   div.style.height = '100px';};#googleMap {    height: 100%;  }html, body {    height: 100%;    margin: 0;    padding: 0;  }
查看完整描述

1 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

窗格上的元素overlayLayer不接收 DOM 事件(如click),而是將您的按鈕附加到overlayMouseTarget窗格。

文檔

overlayLayer
類型:元素
此窗格包含折線、多邊形、地面疊加層和平鋪層疊加層。它不接收 DOM 事件。(窗格 1)。

改變:

panes.overlayLayer.appendChild(div);

至:

panes.overlayMouseTarget.appendChild(div);

代碼片段:

var map;


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

var chicago = { lat: 41.85, lng: -87.65 };


CustomImageOverlay.prototype = new google.maps.OverlayView();


function GoogleMap() 

{

  var pos =   { lat: 62.323907, lng: -150.109291}

  map = new google.maps.Map(document.getElementById('googleMap'), {

    center: pos,

    zoom: 8

  });


  overlay = new CustomImageOverlay(map, pos);


}



function CustomImageOverlay(map, position) 

{

  this.map_ = map;

  this.position_ = position;


  this.div_ = null;


  this.setMap(map);

}


CustomImageOverlay.prototype.onAdd = function() {


  var div = document.createElement('div');

  div.style.borderStyle = 'solid';

  div.style.borderWidth = '0px';

  div.style.position = 'absolute';

  div.style.cursor = "pointer";



  // Create Button

  var button = document.createElement("button");

  button.innerHTML = "Click Here!";

  button.style.position = 'absolute';

  button.style.cursor = "pointer";

  div.appendChild(button);


  button.addEventListener("click", function() {

    console.log("click");

    map.setCenter(chicago);

  });



  this.div_ = div;


  // Add the element to the "overlayLayer" pane.

  var panes = this.getPanes();

  panes.overlayMouseTarget.appendChild(div);

};



CustomImageOverlay.prototype.draw = function() {


  var overlayProjection = this.getProjection();


  var div = this.div_;

  

  var userLatLng = new google.maps.LatLng(this.position_);

  var posToPix = overlayProjection.fromLatLngToDivPixel(userLatLng);


   div.style.left = posToPix.x + 'px';

   div.style.top = posToPix.y + 'px';


   div.style.width = '100px';

   div.style.height = '100px';

};

#googleMap {

    height: 100%;

  }


html, body {

    height: 100%;

    margin: 0;

    padding: 0;

  }

<!DOCTYPE html>

<html>


    <head>

        <title>Title</title>

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

    </head>


<body>


    <div id="googleMap"></div>


    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>


</body>

</html>


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號