javascript “具有引用者限制的API密钥不能与此API一起使用,”

6l7fqoea  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(142)

我一直在尝试访问地理编码和地理定位,它总是给我不同的错误,每次。我目前的错误是:
Uncaught(in promise)Error at _.se.captureStackTrace
当我打印promise的响应时,它会打印一个带有以下错误消息的数组:
带referer限制的API密钥不能用于此接口。
此外,我在尝试使用几何体时也会遇到此错误:
未捕获(承诺中)TypeError:无法读取未定义的属性(阅读“geometry”)
下面是我的代码在我的头:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MyAPIKey&callback=initMap&v=beta&libraries=geometry"></script>
    <script src="https://unpkg.com/axios@1.1.2/dist/axios.min.js"></script>

然后在我的body后面添加代码:

function initMap(id, addy, city, state){
      var x = document.getElementById(id);
      
      if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      }
      else{ 
        x.innerHTML = "Geolocation is not supported by this browser.";
      }

      var location = addy + " " + city + "  " + state;
        axios.get('https://maps.googleapis.com/maps/api/geocode/json',{
            params:{
            address: location,
            key: 'MyAPIKey',
            libraries: "geometry"
          }
      })
      .then(function(response){
        console.log(response);
        latLngA = new google.maps.LatLng(response.data.results[0].geometry.location.lat, response.data.results[0].geometry.location.lng);
        latLngB = new google.maps.LatLng(a,b);
        x.innerHTML = (google.maps.geometry.spherical.computeDistanceBetween(latLngA, latLngB)/1000).toFixed(2) + "km";
      })
    }
    
    window.initMap = initMap;
iswrvxsc

iswrvxsc1#

要使用此Google Maps API密钥,请打开您的GCP containing this key,删除此密钥属性Key restrictions - Set an application restriction中的引用人限制并保存更改。

相关问题