I have a Angular controller showing a Google Map (via NgMap). On the map i can add markers by clicking on the map. When clicking, a marker is set at the position and a infoWindows is opened. The window contains a link to a javascript function.
My problem is, that i can only get this to work, as long as my function (goto) is outside of my angular controller. But i need to reference some angular methods and properties within my goto function (like $scope etc.).
What am i missing?
My code:
(function () {
'use strict';
myApp.controller('myController', function myController($scope, $http, $window, NgMap) {
var vm = this;
NgMap.getMap({ id: 'myMap' }).then(function (map) {
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'geometry',
from: 'myid'
}
map: map,
suppressInfoWindows: true
});
layer.addListener('click', function (e) {
var marker = new google.maps.Marker({ position: e.latLng, map: map });
map.panTo(e.latLng);
windowControl(e, infoWindow, map, marker);
});
});
function windowControl(e, infoWindow, map, marker) {
geocoder.geocode({ 'latLng': e.latLng },
function (results, status) {
var location;
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
location = results[0].formatted_address;
var windowcontent = location + "<br/><br/>";
windowcontent += "<button data-latlng='" + e.latLng + "' data-location='" + location + "' onclick='goto(this);'>GO</button>";
infoWindow.setOptions({
content: windowcontent
});
infoWindow.open(map, marker);
} else {
location = "No results";
}
} else {
location = status;
}
});
}
});
})();
function goto(thisObj) {
var latLng = thisObj.getAttribute( "data-latlng" );
var location = thisObj.getAttribute( "data-location" );
console.log(latLng + ' - ' + location);
}