I am using AngularJS, Ionic Framework and Cordova. I tried to set current user location in the center of the map.
Here is my code:
.controller('mainCtrl', function($scope) {
$scope.position = "";
var latLng = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$scope.$apply(function() {
$scope.position = position;
latLng = {
'lat': position.coords.latitude,
'lng': position.coords.longitude
};
});
});
};
$scope.map = {
center: {
latitude: 51.219053,
longitude: 4.404418
},
...
I can print current user location this way outside the script:
clat: {{position.coords.latitude}}, clang: {{position.coords.longitude}}
But I want to use it inside the script. I have tried these without any success:
center: { latitude: position.coords.latitude,...
center: { latitude: $scope.position.coords.latitude,...
center: { latitude: latLng.lng,...
center: { latitude: latLng['lng'],...
How can use it inside the script?
Error log:
TypeError: Cannot read property 'latitude' of undefined
$apply
necessary when the function call takes place within a controller?