谷歌地图API不再工作

google map api not working anymore

本文关键字:工作 不再 API 谷歌地图      更新时间:2023-09-26

这段代码直到今天都运行良好:

navigator.geolocation.getCurrentPosition(
    function (pos) {
        var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
        console.log('ok');
        geocoder = new google.maps.Geocoder();            
        geocoder.geocode({'latLng': latlng}, function (results, status) {
            console.log('callback');
            console.log(status);
        });
    },
    errorGetCurrentPositionCallback,
    {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000});        

我可以在控制台中看到"ok"消息,但看不到"回调"。完全没有错误,我使用了配额的 0%。有些东西发生了变化,但在我的代码中没有。控制台.log不再到达。你有什么想法吗?

您的函数会检查导航器是否可以找到 GPS 位置(这纯粹是设备/浏览器功能,而不是 Google 服务)。但是您的函数不会检查地理编码器的状态(这是一项 Google 服务)。

看看你的控制台是这样的:

navigator.geolocation.getCurrentPosition(
  function (pos) {
      var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
      console.log('Just before geocode is called');
      geocoder = new google.maps.Geocoder();            
      geocoder.geocode({'latLng': latlng}, function (results, status) {
          console.log('callback of geocoder called');
          if (status == google.maps.GeocoderStatus.OK) {
            console.log('Status is okay');
          }
          else {
            console.log('Status is not okay, the reason: ' + status);
          }
      });
  },
  errorGetCurrentPositionCallback,
  {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
);

这是一个功能示例

<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    var map;
    function initialize() {
      var latlng = new google.maps.LatLng(50.45, 4.45);    // set your own default location.  This gets called if the parameters are left blank.
      var myOptions = {
        zoom: 15,
        center: latlng
      };
      map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    }
    function errorGetCurrentPositionCallback() {
      console.log('Your device cannot find your location');
    }
    // initiate Google Maps + request the position of the user.
    function page_loaded() {
      initialize();
      navigator.geolocation.getCurrentPosition(
        // success callback
        function (pos) {
          var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
          var accuracy = pos.coords.accuracy;
          map.setCenter(latlng);
          var marker = new google.maps.Marker({
            position: latlng, 
            map: map, 
            title: "You are here! (at least within a " + accuracy + " meter radius)"
          });
          console.log('Just before geocode is called');
          geocoder = new google.maps.Geocoder();            
          geocoder.geocode({'latLng': latlng}, function (results, status) {
            console.log('callback of geocoder called');
            if (status == google.maps.GeocoderStatus.OK) {
              console.log('Status is okay; you have the address of this location');
              // 
            }
            else {
              console.log('Status is not okay, the reason: ' + status);
            }
          });
        },
        errorGetCurrentPositionCallback,
        {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
      );
    }
    google.maps.event.addDomListener(window, 'load', page_loaded);
</script>
<style>
#map-canvas {
  width: 500px;
  height: 400px;
}
</style>
<div id="map-canvas"></div>