The following code finds the current users iPhone geolocation on load and loads a google map accordingly using a directive.
Unfortunately the geolocationData data variable from the directive does not get shared to the html footer {{geolocationData}}...
So how can I pass in this case the geolocationData across from the directive to the html page and/or the controller.
Thank you.
Directive:
'use strict';
angular.module('MyApp.directives', ['ngCordova'])
.directive('map', function($cordovaGeolocation) {
return {
restrict: 'E',
scope: {
onCreate: '&'
},
link: function ($scope, $element, $attr) {
function initialize() {
if ( !$scope.geolocationData ) {
$scope.geolocationData = {};
}
$cordovaGeolocation
.getCurrentPosition()
.then(function (position) {
var lat = position.coords.latitude
$scope.geolocationData.latitude = lat;
var long = position.coords.longitude
$scope.geolocationData.longitude = long;
console.log("POSITION: ", lat, long); //works
var mapOptions = {
center: new google.maps.LatLng(lat, long),
// center: $scope.geolocationCenter,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($element[0], mapOptions);
$scope.onCreate({map: map});
// Stop the side bar from dragging when mousedown/tapdown on the map
google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
e.preventDefault();
return false;
});
}, function(err) {
// error
});
}
google.maps.event.addDomListener(window, 'load', initialize);
}
};
});
And the following controller:
'use strict';
angular.module('MyApp.controllers', ['ngCordova'])
.controller('MapCtrl', function($scope, $ionicLoading, $cordovaGeolocation) {
if ( !$scope.geolocationData ) {
$scope.geolocationData = {};
}
};
And the following html snippet:
<body ng-app="MyApp" ng-controller="MapCtrl">
<!-- MAP AREA -->
<ion-content scroll="false" style="bottom:50%;">
<map on-create="mapCreated(map)"></map>
</ion-content>
<ion-footer-bar class="bar-stable">
{{geolocationData}}
</ion-footer-bar>
</body>