I'm getting the following error trying to use the geocoder service for the ng-map library:
angular.js:13642 ReferenceError: google is not defined
at Object.t [as geocode] (http://localhost:6240/Scripts/ng-map.min.js:25:28246)
I'm injecting the service into my controller
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);
});
}]);
I also tested it with NgMap function getting the same reference error
NgMap.getGeoLocation('Avenida Calle 26 # 40-40, Bogotá').then(function (result) {
console.log('GETGEOLOCATION: ', result);
}, function (error) {
console.log('Error getting geolocation: ', error);
});
As shown in the snippet, I have successfully used other NgMap and NavigatorGeolocation services.
Here is the code of my page
<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>
And in the corresponding controller/component
$ctrl.googleMapsUrl = "https://maps.googleapis.com/maps/api/js?key=MY-API-KEY";
$ctrl.mapCenter = 'current-location';
NavigatorGeolocation.getCurrentPosition({ timeout: 10000 }).then(function (ll) {
$ctrl.svp = "StreetViewPanorama(document.querySelector('ng-map#sv'), {position:new google.maps.LatLng(" + ll.coords.latitude + " , " + ll.coords.longitude + ")})";
}, function (error) {
console.log('error getCurrentPosition: ', error);
});
I'm using AngularJS v1.5.6
Thanks in advance for your help.
GeoCoder
service utilizes google.maps.Geocoder class
which in turn is a part of Google Maps API. The moment when GeoCoder.geocode
method is invoked in controller Google Maps library is not yet loaded (in your case it is loaded asynchronously via map-lazy-load
directive), that's the reason why this error occurs.
You could use NgMap.getMap
function to guarantee Google Maps API is ready:
NgMap.getMap("map").then(function () {
GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
.then(function (result) {
//...
});
});
Demo
angular.module('mapApp', ['ngMap'])
.controller('mapCtrl', ['$scope', 'NgMap', 'NavigatorGeolocation', 'GeoCoder',
function ($scope, NgMap, NavigatorGeolocation, GeoCoder) {
vm = this;
NgMap.getMap("map").then(function () {
GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
.then(function (result) {
vm.mapCenter = result[0].geometry.location;
});
});
vm.googleMapsUrl = "https://maps.googleapis.com/maps/api/js";
vm.mapCenter = null;
}]);
<script src="https://code.angularjs.org/1.5.6/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<div ng-app="mapApp" ng-controller="mapCtrl as vm">
<div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{ vm.googleMapsUrl }}">
<ng-map id="map" center="{{ vm.mapCenter }}"></ng-map>
</div>
</div>