angular-ui / ui-leaflet

AngularJS directive to embed an interact with maps managed by Leaflet library
http://angular-ui.github.io/ui-leaflet
Other
314 stars 134 forks source link

Cannot read property 'centerMapOnPosition' of undefined #91

Open nmccready opened 9 years ago

nmccready commented 9 years ago

From @VolvoOlympian on July 18, 2015 8:16

I get "Cannot read property 'centerMapOnPosition' of undefined" with the following code on line L.GeoIP.centerMapOnPosition(map, 15);, am I missing something?

.controller('CourseDetailsCtrl', ['$scope', 'leafletData', function($scope, leafletData) { leafletData.getMap('mymap').then(function(map) { L.GeoIP.centerMapOnPosition(map, 15); }); } ])

Copied from original issue: tombatossals/angular-leaflet-directive#856

nmccready commented 9 years ago

From @sibbl on July 18, 2015 8:29

Inject $window into your function and then use $window.L to access the leaflet namespace.

nmccready commented 9 years ago

From @VolvoOlympian on July 18, 2015 9:2

Like this? .controller('CourseDetailsCtrl', ['$scope', 'leafletData', function($scope, $window, leafletData) { leafletData.getMap('mymap').then(function(map) { $window.L.GeoIP.centerMapOnPosition(map, 15); }); } ])

But it does not work...sorry I am quite new to angular

nmccready commented 9 years ago

From @sibbl on July 18, 2015 13:18

You need to add '$window' to the list before your function as well:

.controller('CourseDetailsCtrl', ['$scope', '$window', 'leafletData', function($scope, $window, leafletData) { leafletData.getMap('mymap').then(function(map) { $window.L.GeoIP.centerMapOnPosition(map, 15); }); } ])

See the angular docs about dependency injection: https://docs.angularjs.org/guide/di

nmccready commented 9 years ago

Is this resolved?

Moulde commented 8 years ago

I don't know if we have the same issue, but i get the exact same error message. Please correct me if i'm wrong, but doesnt the error message say that the 'centerMapOnPosition' function can not be found on undefined, which means that L is correctly referenced, but that the GeoIP property is undefined, not the L property.

L contains a bunch of properties, but not the GeoIP one on which the centerMapOnPosition function should be accessible.

The code is taken directly from the sample in the github readme. Anyone know how to solve this error?