简体   繁体   English

Google Maps Geocoding API“此服务需要API密钥”

[英]Google Maps Geocoding API “This service requires an API key”

I am trying to use Google geocoding API. 我正在尝试使用Google地理编码API。 However I am getting following error even after mentioning API key This service requires an API key. 但是,即使提到API密钥后,我仍然出现以下错误。 此服务需要API密钥。 For more information on authentication and Google Maps JavaScript API 有关身份验证和Google Maps JavaScript API的更多信息

Following is my HTML entry with API key: 以下是我的带有API密钥的HTML条目:

<script src="//maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyAa7LqHyZpHtQBGR6415pYu1FnwWQBPcnY" type="text/javascript"></script>

and this is the angularjs code where I am getting REQEST_DENIED 这是我得到REQEST_DENIED的angularjs代码

    function geocode(dataset){
var coords = [];
var address = dataset.address;
var geocoder = new google.maps.Geocoder();
var defer = $q.defer();
geocoder.geocode({'address': address}, function( results, status ) {
        if (status === google.maps.GeocoderStatus.OK) {
            coords[0] = results[0].geometry.location.lat();
            coords[1] = results[0].geometry.location.lng();
            dataset["coordinates"]=results[0].geometry.location;
            defer.resolve(dataset);
        }
        else {
            coords = 'Could not retrieve coordinates for: ' + address;
            defer.reject();
        }
    });
return defer.promise;
}

Is there something I am missing? 我有什么想念的吗? Thank you. 谢谢。

So as per the comments above, when I checked network layer after executing fiddle from @FadiAboMsalam I could see the API key being sent into the request URL which was missing in my request. 因此,按照上面的评论,当我在执行@FadiAboMsalam的提琴后检查了网络层时,我可以看到API密钥被发送到了请求URL中,而请求URL在我的请求中丢失了。 Am not sure why geocoder.geocode(....) is not sending api ley in my request. 不知道为什么geocoder.geocode(....)没有在我的请求中发送api ley。 I changes this code to send $http request and it is giving proper repsonse. 我更改此代码以发送$ http请求,并且给出了适当的回复。 Here is the updated code: 这是更新的代码:

  function geocode(dataset){
    var coords = [];
    var address = dataset.address;
    var geocoder = new google.maps.Geocoder();
    var defer = $q.defer();


 $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + 
            address + '&key=xxxxxxxx')
    .then(function(results){
     if (results.status === 200) {
        var data = results.data.results;
            dataset["coordinates"]=data[0].geometry.location;
            defer.resolve(dataset);
         }
         else {
             coords = 'Could not retrieve coordinates for: ' + address;
             defer.reject();
         }
     },
     function error(_error){
        defer.reject();
     });

    return defer.promise;
}

Thanks @PaulThomasGC and @FadiAboMsalam for useful inputs and others for investing your precious time. 感谢@PaulThomasGC和@FadiAboMsalam提供了有用的意见,以及其他一些宝贵的时间。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM