Google 地图地理定位未请求许可

Google maps Geolocation not asking for permission

我正在尝试在以下页面中使用 Google 地图 API: https://developers.google.com/maps/documentation/javascript/geolocation

我所做的只是复制该页面上的代码并将其粘贴到我网站上的测试页面上。 http://kwt-events-com.stackstaging.com/content/api/maps.php

我有一把 API 钥匙。我使用了它,页面加载 google 地图时效果很好。

问题是它无法检测到显示此错误的位置: "Error: The Geolocation service failed"

我试过的: 是允许在浏览器和计算机中进行位置检测。 我在同一台机器上尝试了不同的浏览器 "macos"。 我在另一台机器上试过 "another pc and another mac"。 我试过 phone "ios".

同样的错误

我知道我不能 post 所有的代码但是我会这样做所以你们可以帮助我

<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
  /* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
 </style>
 </head>
 <body>
 <div id="map"></div>
 <script>
  // 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.
  var map, infoWindow;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 6
    });
    infoWindow = new google.maps.InfoWindow;

    // 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.');
        infoWindow.open(map);
        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.');
    infoWindow.open(map);
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=IHAD-MY-API-KEY-HERE&callback=initMap">
</script>

您的页面是通过 HTTP 而不是 HTTPS 提供的,并且只有在自 Chrome 50、Safari 10 和 Firefox 55 以来安全提供页面的情况下才能使用地理定位。

首先,您的页面应该是 https。 Google 地理位置仅适用于 https。

你也可以使用这个简单的代码

    function getLocation() {
     if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(showPosition);
     } else { 
      x.innerHTML = "Geolocation is not supported by this browser.";
     }
   }
 getLocation();

   function showPosition(position) { 
   var latitude = position.coords.latitude; 
   var longitude = position.coords.longitude; 
   console.log(latitude,longitude);
  }