地理位置无法在Chrome中运行 [英] Geo Location not working in Chrome

查看:159
本文介绍了地理位置无法在Chrome中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只使用来自以下URL的代码(带有和不带有API),但是该位置未在Chrome上加载.在IE和Firefox上也可以正常工作.

I am just using the code from the following URL with and without API, however the location is not loading on Chrome. The same works fine on IE and Firefox.

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

Can anyone help me understand whats the issue. Please note Location is turned on under the Chrome Settings

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

在这方面的任何帮助都将受到赞赏

Any help in this regard is appreciated

<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的任何API(地图),则需要先获取

If you plan to use any of Google's API, Maps for this instance, you need to obtain an API Key first as stated in the Maps JavaScript API. Make sure that it is of Browser type. .

因此,在您的情况下,请将YOUR_API_KEY替换为从Google Developer Console中获得的内容,例如:

So in your case replace the YOUR_API_KEY with the one obtained in your Google Developer Console like:

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

此外,请不要忘记注册URL来源.这可以是本地主机或已部署的URL,例如:

Also, dont forget to register the URL origin. This can either be a localhost or a deployed URL, e.g.:

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

这篇关于地理位置无法在Chrome中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆