【问题标题】:Google map api usage for geolocation I dont know whats wrong?谷歌地图 api 用于地理定位我不知道有什么问题?
【发布时间】:2014-01-16 16:57:20
【问题描述】:

我已经从谷歌的教程中复制了确切的代码并添加了我的 api 密钥所以从代码的角度来看不会有任何错误。但是我的浏览器不允许地理定位,所以那里一定有一些错误。谁能告诉我我错过了什么。以下是一些屏幕截图,可能有助于理解我的问题。

此页面默认允许我的位置设置

我的 Google 项目显示所有谷歌地图和地理定位服务都已开启

加载页面时出现错误对话框

在单击该管理设置时,我还检查了我是否为所有人打开了位置

我真的不知道怎么了?如果有任何用途,这是代码 - 来自

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

<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <!--
    Include the maps javascript with sensor=true because this code is using a
    sensor (a GPS locator) to determine the user's location.
    See: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API
    -->
    <script src="https://maps.googleapis.com/maps/api/js?key={My_API_KEY}&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
  };
  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="map-canvas"></div>
  </body>
</html>

请注意,它在 Mozilla Firefox 中可以正常工作,但不能在 chrome 中运行

【问题讨论】:

    标签: google-maps geolocation


    【解决方案1】:

    出于安全原因,Chrome 会为来自具有 file:\\ 方案的文件系统的资源禁用 GeoLocation。如果你在 IIS 中托管你的页面,你应该很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2013-02-17
      • 1970-01-01
      • 2017-12-02
      • 2015-03-01
      • 1970-01-01
      相关资源
      最近更新 更多