I have a directive in one of my templates and here is the code for that directive:
appDirectives.directive('map', function() {
return {
restrict: 'E',
scope: {
onCreate: '&'
},
link: function ($scope, $element, $attr) {
alert("This alert works fine.");
function initialize() {
alert("This alert doesn't appear on Phonegap.");
navigator.geolocation.getCurrentPosition(function (pos) {
$scope.currentLocation = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var mapOptions = {
center: $scope.currentLocation,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
var map = new google.maps.Map($element[0], mapOptions);
var currentLocation = $scope.currentLocation;
$scope.onCreate({
map: map,
currentLocation: currentLocation
});
// Prevent sidebar drag when clicking on the map
google.maps.event.addDomListener($element[0], 'mousedown', function (e) {
e.preventDefault();
return false;
});
}, function (error) {
alert('Error getting location.');
});
}
google.maps.event.addDomListener(window, 'load', initialize());
}
}
When I run the app on the browser, everything functions as expected. However, when running on the iOS Simulator, the initialize() function fails to execute.
I attempted this fix (as mentioned here):
google.maps.event.addDomListener(window, 'load', initialize);
But unfortunately, this did not work in either the browser or the simulator.
Any insights into why this might be happening?