javascript - 如何在 Google Maps API 上定位自定义按​​钮控件?

标签 javascript html css google-maps google-maps-api-3

我在 Google Maps API v3 上构建了这张简单的 map ....

<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {



      var myOptions = {
          zoom: 8,
          center: {lat: -34.397, lng: 150.644},
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map"), myOptions);


      //### Add a button on Google Maps ...
      var controlMarkerUI = document.createElement('DIV');
      controlMarkerUI.style.cursor = 'pointer';
      controlMarkerUI.style.backgroundImage = "url(http://localhost/marker.png)";
      controlMarkerUI.style.height = '28px';
      controlMarkerUI.style.width = '25px';
      controlMarkerUI.style.top = '11px';
      controlMarkerUI.style.left = '120px';
      controlMarkerUI.title = 'Click to set the map to Home';
      //myLocationControlDiv.appendChild(controlUI);

      map.controls[google.maps.ControlPosition.LEFT_TOP].push(controlMarkerUI);

      //### Add a button on Google Maps ...
      var controlTrashUI = document.createElement('DIV');
      controlTrashUI.style.cursor = 'pointer';
      controlTrashUI.style.backgroundImage = "url(http://localhost/trash.png)";
      controlTrashUI.style.height = '28px';
      controlTrashUI.style.width = '25px';
      controlTrashUI.style.top = '11px';
      controlTrashUI.style.left = '150px';
      controlTrashUI.title = 'Click to set the map to Home';
      //myLocationControlDiv.appendChild(controlUI);

      map.controls[google.maps.ControlPosition.LEFT_TOP].push(controlTrashUI);
}

    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=<PUT_YOUR_API-HERE>&callback=initMap"
    async defer></script>
  </body>
</html>

map 结果如下

enter image description here

在我的代码中,标记和垃圾桶这两个按钮的样式设置方式应该以这种方式放在“卫星”按钮旁边

enter image description here

为什么我的样式设置没有保留?

建议/示例?

最佳答案

我有一个答案,不是 100% 如果这是你要找的,但这是修改后的代码。

<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
  /* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
 </style>
</head>
<body>

<div id="map"></div>

<script>
  var map;
  function initMap() {

      var myOptions = {
          zoom: 8,
          center: {lat: -34.397, lng: 150.644},
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map"), myOptions);

      // Create a div to hold the control.
      var controlDiv = document.createElement('div');

      //### Add a button on Google Maps ...
      var controlMarkerUI = document.createElement('div');
      controlMarkerUI.style.cursor = 'pointer';
      controlMarkerUI.style.backgroundColor = 'blue';
      controlMarkerUI.style.height = '28px';
      controlMarkerUI.style.width = '25px';
      controlMarkerUI.style.marginLeft = '10px';
      controlMarkerUI.style.marginTop = '10px';
      controlMarkerUI.title = 'Click to set the map to Home';
      controlDiv.appendChild(controlMarkerUI);


      //### Add a button on Google Maps ...
      var controlTrashUI = document.createElement('div');
      controlTrashUI.style.cursor = 'pointer';
      controlTrashUI.style.backgroundColor = 'black';
      controlTrashUI.style.height = '28px';
      controlTrashUI.style.width = '25px';
      controlTrashUI.style.marginLeft = '60px';
      controlTrashUI.title = 'Click to set the map to Home';
      controlMarkerUI.appendChild(controlTrashUI);

      map.controls[google.maps.ControlPosition.TOP_LEFT].push(controlDiv);

}

</script>
    <script src="https://maps.googleapis.com/maps/api/js?key=<PUT KEY 
 HEREEEE>&callback=initMap"
async defer></script>
  </body>
</html>

你必须把你的标志改回你的本地主机我想这个 是做你想做的最好的方法。不要忘记您的 API key !!

输出: the result

关于javascript - 如何在 Google Maps API 上定位自定义按​​钮控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44555344/

相关文章:

javascript - 错误 : Network error: Error writing result to store for query (Apollo Client)

php - 为什么在启用 Javascript 时不忽略 <noscript> 标记内的所有内容?

html - 如何在一组容器中调整不同大小的图像大小 | CSS

html - 创建一个标题,下面有两条彩色线条

javascript - React : Paginated table vs Scrollable table for column sort 中的速度比较

javascript - 处理验证的最佳实践

html - 为特定元素定义 css

javascript - 字母正则表达式 + 数值

html - 谁能告诉我主 div 中这两个图像集的区别?

javascript - @ViewChild 为空