I'm trying to change few options of Google Maps drawing manager by clicking a button using Angular. But I receive error messages saying that DrawingManager variable is undefined.
I initialized Google Maps and DrawingManager in ngAfterViewInit function in an Angular Component. Google Map object and the Drawing Manager object are assigned to variables in the component as shown in the code. I want to change the few options of Drawing Manager by Clicking a button in the component. "drawControlEnable" function is called by clicking the button. But DrawingManager object is undefined inside the "drawControlEnable" function. I have put the Google Maps Script in the index.html and the code for the component.
Index.html
<script async defer src="https://maps.googleapis.com/maps/api/js?key=XXXXX&libraries=drawing,geometry"></script>
Angular Component
import {Component, OnInit, ViewChild, AfterViewInit} from '@angular/core';
import { } from '@types/googlemaps';
.....
.....
@ViewChild('gmap') gmapElement: any;
map: google.maps.Map;
shapes = [];
coordinatesArray = [];
drawingManager: any;
ngAfterViewInit() {
var mapProp = {
center: new google.maps.LatLng(7.515073, 80.723066),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
this.drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: null,
drawingControl: false,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: ['polygon']
},
markerOptions: {icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'},
polygonOptions: {
editable: true,
draggable: true
}
});
this.drawingManager.setMap(this.map);
}
.....
.....
drawControlEnable() {
this.drawingManager.setDrawingMode(google.maps.drawing.OverlayType.MARKER);
this.drawingManager.setOptions({drawingControl : true});
}
How I can change DrawingManager options by clicking a button?