简体   繁体   English

Google Maps DrawingManager在Angular中未定义

[英]Google Maps DrawingManager is undefined in Angular

I'm trying to change few options of Google Maps drawing manager by clicking a button using Angular. 我试图通过单击使用Angular的按钮来更改Google Maps绘图管理器的一些选项。 But I receive error messages saying that DrawingManager variable is undefined. 但是我收到错误消息,说DrawingManager变量未定义。

I initialized Google Maps and DrawingManager in ngAfterViewInit function in an Angular Component. 我在Angular组件的ngAfterViewInit函数中初始化了Google Maps和DrawingManager。 Google Map object and the Drawing Manager object are assigned to variables in the component as shown in the code. 如代码所示,将Google Map对象和Drawing Manager对象分配给组件中的变量。 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. 单击该按钮可调用“ drawControlEnable”功能。 But DrawingManager object is undefined inside the "drawControlEnable" function. 但是在“ drawControlEnable”函数中未定义DrawingManager对象。 I have put the Google Maps Script in the index.html and the code for the component. 我已经将Google Maps脚本放在index.html和该组件的代码中。

Index.html 的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? 如何通过单击按钮来更改DrawingManager选项?

This is how i create a map and change the drawing control option with a button: First I declare two variables: one for the map and one for the drawingManager 这是我创建地图并通过按钮更改绘图控制选项的方式:首先,我声明两个变量:一个用于地图,一个用于drawingManager。

  theMap: any;
  drawingManager: any;

the I create the map in ngOnInit and call the drawingManager: 我在ngOnInit中创建地图并调用drawingManager:

this.theMap = this.initMap(lat, lng);

if(this.theMap) {
      this.initDrawingManager(this.theMap)
}

the two functions for creating the map and initializing the drawing manager are: 用于创建地图和初始化图形管理器的两个功能是:

  initMap(lat, lng) {
    let mapProp = {
      center: new google.maps.LatLng(lat, lng),
      zoom: 16,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    let map = new google.maps.Map(document.getElementById("map"), mapProp);

    let marker = new google.maps.Marker({
      icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
      animation: google.maps.Animation.DROP,
      position: { lat: lat, lng: lng },
      map: map
    })

    return map;
  }

and

  initDrawingManager(map) {
    this.drawingManager = new google.maps.drawing.DrawingManager({
      // drawingMode: google.maps.drawing.OverlayType.MARKER,
      drawingControl: false,
      drawingControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER,
        drawingModes: ['circle', 'polygon']
      },
      // markerOptions: {icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'},
      polygonOptions: {
        draggable: true,
        editable: true
      },
      circleOptions: {
        // fillColor: '#ffff00',
        // fillOpacity: 1,
        // strokeWeight: 5,
        draggable: true,
        clickable: true,
        editable: true,
        zIndex: 1
      }
    });

    this.drawingManager.setMap(map);
  }

The function that changes the drawingControl option is: 更改drawingControl选项的函数是:

  change() {
    this.drawingManager.setOptions({
      drawingControl: true
    });
  }

and the HTML is: HTML是:

<button
   (click)="change()">
   change
</button>

By clicking the button, the drawing controls will appear on the map. 通过单击按钮,绘图控件将出现在地图上。

Our difference is that I create the map in ngOnInit. 我们的区别是我在ngOnInit中创建了地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM