I changed a few things to make this work:
You no longer need the api key for Google maps, this is enough for the script src (see What is the Google Maps API Key ):
<script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
Replace "function initialize ()" with "$ scope.init ()" and comment out the line where it is:
//google.maps.event.addDomListener(window, 'load', initialize);
Also add ng-init = "init ()" to your html as follows:
<ion-header-bar class="bar-dark" ng-init="init()"> <h1 class="title">Map</h1> </ion-header-bar>
I do not know why it does not work with domListener, but the data must be initialized before displaying it (see Google MAP API Uncaught TypeError: it is impossible to read the 'offsetWidth' property from zero ). With ng-init you can achieve this.
The final controller should look like this:
.controller('MapCtrl', function($scope, $ionicLoading, $compile) { $scope.init = function() { var myLatlng = new google.maps.LatLng(43.07493,-89.381388); var mapOptions = { center: myLatlng, zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map"), mapOptions); //Marker + infowindow + angularjs compiled ng-click var contentString = "<div><a ng-click='clickTest()'>Click me!</a></div>"; var compiled = $compile(contentString)($scope); var infowindow = new google.maps.InfoWindow({ content: compiled[0] }); var marker = new google.maps.Marker({ position: myLatlng, map: map, title: 'Uluru (Ayers Rock)' }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); $scope.map = map; }; // google.maps.event.addDomListener(window, 'load', initialize); $scope.centerOnMe = function() { if(!$scope.map) { return; } $scope.loading = $ionicLoading.show({ content: 'Getting current location...', showBackdrop: false }); navigator.geolocation.getCurrentPosition(function(pos) { $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)); $scope.loading.hide(); }, function(error) { alert('Unable to get location: ' + error.message); }); }; $scope.clickTest = function() { alert('Example of infowindow with ng-click') }; });
Hope this helps!
error1337
source share