【发布时间】:2014-06-05 22:25:28
【问题描述】:
我的一个模板中有一个指令,以下是该指令的代码:
appDirectives.directive('map', function() {
return {
restrict: 'E',
scope: {
onCreate: '&'
},
link: function ($scope, $element, $attr) {
alert("This fires just fine.");
function initialize() {
alert("This doesnt fire 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
});
// 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 (error) {
alert('Erro ao obter localização.');
});
}
google.maps.event.addDomListener(window, 'load', initialize());
}
}
在浏览器上运行应用程序时,一切正常。但是在 iOS 模拟器上运行时,它不会触发函数 initialize()。
我已经尝试过了(如 here 所述):
google.maps.event.addDomListener(window, 'load', initialize);
那么它在浏览器和模拟器中都无法正常工作。
知道为什么吗?
【问题讨论】:
标签: javascript angularjs google-maps cordova google-maps-api-3