Google发布了API密钥错误

时间:2016-06-25 12:39:09

标签: javascript api google-maps

我正在开发一个项目,我必须使用google places api for web。我正在使用谷歌api的位置名称下拉菜单。当我填写位置名称时,我收到了这个回复。我已经生成了api密钥并将其嵌入到我的代码中,但这似乎并没有起作用。有什么建议吗?

  

/ ** / xdc ._ 62n6tn&& xdc ._ 62n6tn([3,null,null,"此服务需要> API密钥。有关详情,请参阅   有关身份验证和Google Maps Javascript API服务的信息,请参阅:> https://developers.google.com   / maps / documentation / javascript / get-api-key"])

编辑: 好的,这里是代码

<!DOCTYPE html>
<html>
  <head>
  <title>Retrieving Autocomplete Predictions</title>
 <style>
    html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  #map {
    height: 100%;
  }
  #right-panel {
    font-family: 'Roboto','sans-serif';
    line-height: 30px;
    padding-left: 10px;
  }

  #right-panel select, #right-panel input {
    font-size: 15px;
  }

  #right-panel select {
    width: 100%;
  }

  #right-panel i {
    font-size: 12px;
  }
</style>
 </head>
 <body>
   <div id="right-panel">
    <p>Query suggestions for 'Taxi Services near NewY':</p>
    <ul id="results"></ul>
  </div>
  <script>

  function initService() {
    var displaySuggestions = function(predictions, status) {
      if (status != google.maps.places.PlacesServiceStatus.OK) {
        alert(status);
        return;
      }

      predictions.forEach(function(prediction) {
        var li = document.createElement('li');
        li.appendChild(document.createTextNode(prediction.description));
        document.getElementById('results').appendChild(li);
      });
    };

    var service = new google.maps.places.AutocompleteService();
    service.getQueryPredictions({ input: 'Taxi Services near NewY' }, displaySuggestions);
  }
</script>
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIza---------------------&</script>

并且在这里 google api settings

1 个答案:

答案 0 :(得分:1)

我认为你错过了一些步骤。 1.转到Google Developers Console。 2.启用Google Maps Javascript API。 3.然后生成API密钥。 4.然后在本节的代码底部放置该API密钥。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_JAVASCRIPT_API_KEY&libraries=places&callback=initService"
    async defer></script>

如果仔细按照步骤操作,这将100%有效。

相关问题