Javascript ReferenceError:未使用ng map GeoCoder服务定义google
我在尝试将geocoder服务用于时遇到以下错误:Javascript ReferenceError:未使用ng map GeoCoder服务定义google,javascript,angularjs,google-maps,google-maps-api-3,ng-map,Javascript,Angularjs,Google Maps,Google Maps Api 3,Ng Map,我在尝试将geocoder服务用于时遇到以下错误: angular.js:13642引用错误:未定义google at Object.t[作为地理代码](http://localhost:6240/Scripts/ng-map.min.js:25:28246) 我正在将服务注入我的控制器 appModule.controller('MyController', ['$scope', 'NgMap', 'NavigatorGeolocation', 'GeoCoder', functio
angular.js:13642引用错误:未定义google
at Object.t[作为地理代码](http://localhost:6240/Scripts/ng-map.min.js:25:28246)
我正在将服务注入我的控制器
appModule.controller('MyController', ['$scope', 'NgMap', 'NavigatorGeolocation', 'GeoCoder',
function ($scope, NgMap, NavigatorGeolocation, GeoCoder) {
GeoCoder.geocode({address: 'Avenida Calle 26 # 40-40, Bogotá'}).then(function (result) {
//... do something with result
console.log('RESULT GEOCODER: ', result);
});
}]);
我还用NgMap函数测试了它,得到了相同的参考错误
NgMap.getGeoLocation('Avenida Calle 26 # 40-40, Bogotá').then(function (result) {
console.log('GETGEOLOCATION: ', result);
}, function (error) {
console.log('Error getting geolocation: ', error);
});
如代码片段所示,我已经成功地使用了其他NgMap和NavigatorGeolocation服务
这是我的页面代码
<div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{ $ctrl.googleMapsUrl }}">
<div class="row">
<div class="col-md-6">
<ng-map id="map"
center="{{ $ctrl.mapCenter }}"
street-view="{{ $ctrl.svp }}"></ng-map>
</div>
<div class="col-md-6">
<ng-map id="sv" />
</div>
</div>
</div>
我正在使用AngularJSV1.5.6
提前感谢您的帮助。
GeoCoder
服务利用了google.maps.GeoCoder类
,而该类又是谷歌地图API的一部分。在控制器Google Maps库中调用GeoCoder.geocode
方法的时刻尚未加载(在您的情况下,它是通过map lazy load
指令异步加载的),这就是发生此错误的原因
您可以使用NgMap.getMap
函数来保证Google Maps API已准备就绪:
NgMap.getMap("map").then(function () {
GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
.then(function (result) {
//...
});
});
演示
angular.module('mapApp',['ngMap']))
.controller('mapCtrl',['$scope','NgMap','NavigatorGeolocation','GeoCoder',',
功能($scope、NgMap、NavigatorGeolocation、GeoCoder){
vm=这个;
getMap(“map”).then(函数(){
GeoCoder.geocode({地址:波哥大Avenida Calle 26#40-40})
.然后(函数(结果){
vm.mapCenter=result[0].geometry.location;
});
});
vm.googleMapsUrl=”https://maps.googleapis.com/maps/api/js";
vm.mapCenter=null;
}]);代码>
Perfect Vadim,它使用getMap承诺将其封装起来。谢谢这不再有效,因为需要API密钥。有人知道一个完整的、独立的例子吗?
NgMap.getMap("map").then(function () {
GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
.then(function (result) {
//...
});
});