谷歌地图api用于地理定位我不知道什么是错的?

时间:2014-01-16 08:45:14

标签: google-maps geolocation

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

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

My location setting allowed by default to this page

我的Google项目显示所有Google地图和地理位置服务均已启用

My Google Project showing that all google maps and geolocation services are On

加载页面时给出的错误对话框

Error dialog giving me when i load the page

点击管理设置后,我还检查了我为所有

打开了位置

On clicking that manage settings also i checked that i turned ON location for all

我真的不知道什么是错的?这是代码,如果有任何用处 - 来自

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中

1 个答案:

答案 0 :(得分:2)

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