php - 谷歌地图地理定位不请求许可

标签 php html api google-api google-geolocation

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

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

我有一个 API key 。我使用了它,页面加载谷歌地图时效果很好。

问题是它无法检测到显示此错误的位置: “错误:地理定位服务失败”

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

同样的错误

我知道我不能发布所有代码,但我会这样做,所以你们可以帮助我

<!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 之后安全提供页面的情况下,地理定位才可用。

关于php - 谷歌地图地理定位不请求许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52667666/

相关文章:

javascript - 在 PHP 中使用 Jquery 和 Ajax 的动态依赖选择框

PHP-MySQL : Limit data with a specific characters

c# - 如何在 .NET 中调用 DrawThemeTextEx

php - Htaccess 不错的网址

php - Zend Framework 中的大量 DESCRIBE 查询

javascript - 如何在jquery中垂直滚动网页?

html - 网站主要内容旁边的不需要的空间

html - CSS允许元素消失但不会在检查事件中重新出现

api - "API Throttling"和 "API Throttling Warnings"是什么意思?

python - 从 NBA API 收集特定球员信息