我想在我的本地应用中使用 angularjs google places autocomplete 。 在plunker中尝试时代码正常工作,但是当相同的代码被复制到本地应用程序时,它会抛出错误。 Working code demo here.
HTML代码:
<body ng-app="myApp" ng-controller="MainController">
<h1>Hello, World!</h1>
<input type="text" ng-model="search" my-autocomplete>
<p>Search: {{search}}</p>
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script data-require="angular.js@1.3.0-rc2" data-semver="1.3.0-rc2" src="https://code.angularjs.org/1.3.0-rc.2/angular.js"></script>
<script src="script.js"></script>
</body>
的script.js
angular.module('myApp', [])
.controller('MainController', function($scope) {
})
.directive('myAutocomplete', function($log) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
var autocomplete = new google.maps.places.Autocomplete(element[0]);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var address = element.prop('value')
ngModel.$setViewValue(address);
});
}
};
});
以下是在我的本地应用程序中尝试时显示的错误消息:
Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error
_.kb @ js:34
maps.googleapis.com/maps-api-v3/api/js/27/12/util.js:210 Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
iB.j @ maps.googleapis.com/maps-api-v3/api/js/27/12/util.js:210
maps.googleapis.com/maps-api-v3/api/js/27/12/util.js:210 Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
任何建议都有助于解决此错误。下面是本地运行时页面的屏幕截图。
答案 0 :(得分:1)
加载API的脚本元素缺少必需的身份验证参数。如果您使用的是标准Maps JavaScript API,则必须使用带有效API密钥的key参数。基本上你必须传递密钥。请参阅Api文档 - api link