【问题标题】:Geo Location not working in Chrome地理位置在 Chrome 中不起作用
【发布时间】:2023-03-22 11:05:01
【问题描述】:

我只是使用来自以下 URL 的代码,无论是否使用 API,但该位置未在 Chrome 上加载。同样适用于 IE 和 Firefox。

任何人都可以帮助我了解问题所在。请注意,位置信息已在 Chrome 设置下开启

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

感谢您提供这方面的任何帮助

<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.

      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.');
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    </body>
</html>

【问题讨论】:

标签: google-maps google-chrome google-maps-api-3 maps


【解决方案1】:

如果您打算为此实例使用 Google 的任何 API、Maps,您需要先获取 API 密钥,如 Maps JavaScript API 中所述。确保它是浏览器类型。 .

因此,在您的情况下,将 YOUR_API_KEY 替换为您在 Google Developer Console 中获得的密钥,例如:

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap">

另外,不要忘记注册 URL 来源。这可以是 localhost 或已部署的 URL,例如:

http://localhost:3333http://myintranethttp://192.168.1.1http://mywebsite.com

【讨论】:

    【解决方案2】:

    仅适用于 HTTPS!

    [弃用] getCurrentPosition() 和 watchPosition() 不再起作用 关于不安全的起源。要使用此功能,您应该考虑 将您的应用程序切换到安全来源,例如 HTTPS。

    【讨论】:

    • OP 对他们的问题发表了评论,指出他们已经知道这一点。感谢您尝试提供帮助!
    • 抱歉,没有注意到
    【解决方案3】:

    我刚刚在浏览器 URL 中写了 127.0.0.1 而不是 localhost,它对我有用。

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      相关资源
      最近更新 更多