【问题标题】:Geolocation does not work in google chrome地理定位在谷歌浏览器中不起作用
【发布时间】:2016-06-17 11:55:13
【问题描述】:

地理位置是谷歌。示例取自这里 https://developers.google.com/maps/documentation/javascript/examples/map-geolocation?hl=ru 。复制 JAVASCRIPT + HTML 并放置在本地主机上的虚拟主机 http://test.ru 上。创建浏览器密钥并添加它。地图已加载,但未在谷歌浏览器中运行地理定位。告诉我问题是什么。代码如下:

 // Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -34.397, lng: 150.644},
    zoom: 6
  });
  var infoWindow = new google.maps.InfoWindow({map: map});
  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
  var pos = {
    lat: position.coords.latitude,
    lng: position.coords.longitude
  };
  infoWindow.setPosition(pos);
  infoWindow.setContent('Location found.');
  map.setCenter(pos);
}, function() {
  handleLocationError(true, infoWindow, map.getCenter());
});
  } else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
 }
 }
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  infoWindow.setPosition(pos);
  infoWindow.setContent(browserHasGeolocation ?
                    'Error: The Geolocation service failed.' :
                    'Error: Your browser doesn\'t support geolocation.');
}`

【问题讨论】:

  • 您在控制台上遇到任何错误?确保您在 chrome 浏览器上允许定位服务

标签: google-geolocation


【解决方案1】:

问题在于,自 chrome 50 以来,安全更新消除了在没有 https 协议的情况下进行地理定位的可能性。因为你正在尝试这个网址 http://test.ru 它不会工作。 Google details

希望对您有所帮助。 问候。

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 2013-12-28
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多