I'm using javascript Google Maps component in Angular 5 frontend framework
export class MapComponent implements OnInit {
ngOnInit() {
this.initializeGMap()
}
initializeGMap() {
var myLatlng = new google.maps.LatLng(12,77);
var mapOptions = {
zoom: DEFAULT_MAP_ZOOM,
center: myLatlng,
scrollwheel: true,
styles: MAP_STYLE
};
this.map = new google.maps.Map(document.getElementById("map"), mapOptions);
this.initOnMapClickListener();
}
initOnMapClickListener() {
google.maps.event.addListener(this.map, 'click', function(event) {
var selectedLocation = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
this.addMarker(selectedLocation)
});
}
addMarker(latlng) {
var marker = new google.maps.Marker({
position: latlng,
map: this.map,
icon: './assets/map_marker.png'
});
}
}
Above is my typescript file, it has three functions
- initializeGMap() //to initialize google maps
- initOnMapClickListener() //to initialize on map click listener
- addMarker(latlng) // to add the marker when onmapclick event happens
Uncaught TypeError: Cannot read property 'addMarker' of null
this is what the console error I'm getting if I run Angular application
Please help to understand how to call a typescript function inside a Javascript callback