简体   繁体   English

Google Maps API v3地理位置无法在Google Chrome中运行

[英]Google Maps API v3 Geolocation not working in Google Chrome

I'm using Google Maps API v3 Geolocation to get the users actual location. 我正在使用Google Maps API v3地理位置来获取用户的实际位置。 I found this post from Google Developers: 我从Google Developers找到了这篇文章:

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

Here my code (equals to the code of Google Maps example): 这是我的代码(等于Google Maps示例的代码):

<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>

This works quite fine in Firefox but not in Google Chrome & Safari. 这在Firefox中可以正常运行,但在Google Chrome和Safari中则不能。 I think because Chrome & Safari are webkit browsers. 我认为是因为Chrome和Safari是Webkit浏览器。 But I don't know how to fix it. 但是我不知道如何解决。

Can someone please help me? 有人可以帮帮我吗?

I have found 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. 看起来您将需要从服务器本地托管它。

I've tested your code with a local server and it just works fine. 我已经在本地服务器上测试了您的代码,并且工作正常。 Hope I could help. 希望我能帮上忙。

Try to use HTML5 geolocation API instead of Google Maps' one for the browsers you are having problems with. 对于遇到问题的浏览器,请尝试使用HTML5地理位置API,而不要使用Google Maps的API

You can find the reference here 您可以在这里找到参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM