地理位置在谷歌浏览器中不起作用

时间:2016-06-17 11:55:14

标签: google-geolocation

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

 // 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.');
}`

1 个答案:

答案 0 :(得分:0)

问题在于,由于chrome 50,安全更新会在没有http s 协议的情况下消除地理位置的可能性。因为你正在尝试这个网址 http ://test.ru它将无法正常工作。 Google details

希望它有所帮助。 问候。