javascript - Google Maps API v3 Geolocation 在 Google Chrome 中不起作用

标签 javascript google-maps google-maps-api-3 geolocation

我正在使用 Google Maps API v3 Geolocation 获取用户的实际位置。我从 Google Developers 找到这篇文章:

https://developers.google.com/maps/documentation/javascript/examples/map-geolocation

这里是我的代码(等同于谷歌地图示例的代码):

<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>

<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see a blank space instead of the map, this
// is probably because you have denied permission for location sharing.

var map;

function initialize() {
  var mapOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
    <div id="container">
        <div class="entry-content">
            <div class="blogtitle">IT</div><p>&nbsp;</p></td><td> </td><td><p>&nbsp;</p></td></tr></tbody></table><p>&nbsp;</p>
            <div id="map-canvas"></div><!-- map-canvas end -->
        </div><!-- entry-content end -->
    </div><!-- container end -->
</body>

这在 Firefox 中工作得很好,但在 Google Chrome 和 Safari 中却不行。我认为是因为 Chrome 和 Safari 是 webkit 浏览器。但我不知道如何修复它。

有人可以帮帮我吗?

最佳答案

我找到了 this

It appears this is a security restriction for the file protocol. Looks like you are going to need to host it locally from a server.

我已经在本地服务器上测试了您的代码,它运行良好。 希望我能帮上忙。

关于javascript - Google Maps API v3 Geolocation 在 Google Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588940/

相关文章:

javascript - Google Maps Android Studio - 如何禁用 map 控件?

javascript - 当光标靠近 Google Maps API V3 中的 map 边缘时移动 map

javascript - Phonegap 无法读取/呈现 Google Maps API - "Can' t 查找变量 Google"错误

google-maps-api-3 - 如何将地址(作为文本)转换为 GPS 坐标?

javascript - 在将参数传递到 Jade 文件时,我可以(如何)使用 Jade 速记吗?

javascript - 使用 Javascript 将数据存储在大型数组中时的性能问题

javascript - 使用前端 JavaScript 创建自定义 “temperature” map

javascript - 使用 jquery 提交特定的嵌套表单 onkeydown

javascript - 如何解析 wiki 文本表 HTML?

javascript - 如何在java脚本中的设定间隔函数中动态更改谷歌地图标记图标图像?