Google地图API v3地理定位不适用于Google Chrome [英] Google Maps API v3 Geolocation not working in Google Chrome

查看:158
本文介绍了Google地图API v3地理定位不适用于Google Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Maps API v3地理位置来获取用户的实际位置。我从Google Developers发现了这篇文章:

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



这里我的代码(等于Google Maps示例代码):

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

< script>
//注意:此示例要求您在浏览器提示
//时同意位置共享。如果您看到空白区域而不是地图,则此
//可能是因为您拒绝位置共享的权限。

var map;

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

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

//尝试HTML5地理定位
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,
位置:pos,
content:'使用HTML5找到的位置。'
});

map.setCenter(pos);
},function(){
handleNoGeolocation(true);
});
} else {
//浏览器不支持地理定位
handleNoGeolocation(false);



函数handleNoGeolocation(errorFlag){
if(errorFlag){
var content ='错误:地理定位服务失败。
} else {
var content ='错误:您的浏览器不支持地理位置。
}

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为H.;&安培; NBSP;< / P>< / TD>< / TR>< / tbody的>< /表>< p为H.;&安培; NBSP; < / p为H.
< div id =map-canvas>< / div><! - map-canvas end - >
< / div><! - entry-content end - >
< / div><! - container end - >
< / body>

这在Firefox中非常好,但在Google Chrome&苹果浏览器。我认为,因为Chrome& Safari是webkit浏览器。但是我不知道如何解决它。



有人可以帮我吗?

解决方案

我找到了这个


看起来这是文件协议的安全限制。看起来你需要从服务器本地托管它。


我已经使用本地服务器测试了您的代码,只是工作正常。
希望我能帮上忙。


I'm using Google Maps API v3 Geolocation to get the users actual location. I found this post from Google Developers:

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

Here my code (equals to the code of Google Maps example):

<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. I think because Chrome & Safari are webkit browsers. 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.

这篇关于Google地图API v3地理定位不适用于Google Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆