javascript - Google Maps API 相同示例 : one renders, 一个不

标签 javascript html css google-maps

这是两个具有相同代码的 jsfiddle。 One是 Google Maps API 文档的分支,没有变化。 The second只是复制并粘贴到 jsfiddle 中。来自 Google 的分支按预期呈现。我复制粘贴的那个没有。

我使用了差异检查器,它显示它们是相同的。我错过了什么?

HTML

<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDglJmtJY5078jjZOJZFNNMO7CJb5E3nE4&callback=initMap">


</script>

JS

// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -34.397, lng: 150.644},
    zoom: 6
  });
  var infoWindow = new google.maps.InfoWindow({map: map});

  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };

      infoWindow.setPosition(pos);
      infoWindow.setContent('Location found.');
      map.setCenter(pos);
    }, function() {
      handleLocationError(true, infoWindow, map.getCenter());
    });
  } else {
    // Browser doesn't support Geolocation
    handleLocationError(false, infoWindow, map.getCenter());
  }
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  infoWindow.setPosition(pos);
  infoWindow.setContent(browserHasGeolocation ?
                        'Error: The Geolocation service failed.' :
                        'Error: Your browser doesn\'t support geolocation.');
}

CSS

/* 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;
}

最佳答案

在您的第二个 fiddle 中,JAVASCRIPT 设置需要将加载类型设置为 No wrap - in <body>

关于javascript - Google Maps API 相同示例 : one renders, 一个不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40857372/

相关文章:

javascript - 获取范围相对于其父容器的开始和结束偏移量

javascript - Axios 模拟适配器给出错误 'Error: Request failed with status code 404'

javascript - 我的 javascript 代码在 Chrome 和 Firefox 中不起作用

php - 当表格在 Firefox 中跨越多个页面时,不打印单元格边框(第一页除外)

css - 将鼠标悬停在多个并发 DIV 上

html - 无法将我的 html 输入区域居中

javascript - 如何发送从数据库获取的多个结果作为对 POST 的响应?

javascript - 窗口调整大小不起作用

css - 如何使用 CSS 显示 img alt 文本?

javascript - Dreamweaver 不断将外部 CSS 文件与本地服务器上的本地 CSS 一起拉取