Angular2 - google map -drawingManager undefined

时间:2017-03-01 07:48:37

标签: javascript google-maps angular

您好我正在尝试在Google地图中启用绘图工具。但是得到如下错误:

VM1935 zone.js@0.6.25?main = browser:140 Uncaught TypeError:无法读取未定义的属性“DrawingManager”

import { Component } from '@angular/core';
import { Validators, FormGroup, FormArray, FormBuilder } from '@angular/forms';
import { Ng2MapComponent } from 'ng2-map';

Ng2MapComponent['apiUrl'] =
  'https://maps.google.com/maps/api/js?libraries=visualization,places';

@Component({
  selector: 'my-app',
  template: `
    <ng2-map zoom="14" center="Brampton, Canada">
      <marker *ngFor="let pos of positions" 
        (click)="showInfoWindow($event)"
        [position]="pos"></marker>
      <info-window id="iw">
        lat: [[lat]], lng: [[lng]]
      </info-window>
      <drawing-manager
      [drawingMode]="'marker'"
      [drawingControl]="true"
      [drawingControlOptions]="{
        position: 2,
        drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
       }"
      [circleOptions]="{
        fillColor: '#ffff00',
        fillOpacity: 1,
        strokeWeight: 5,
        editable: true,
        zIndex: 1
      }"></drawing-manager>
    </ng2-map>
    <button (click)="showRandomMarkers()">Show Random Markers</button>
  `
})
export class AppComponent {
  positions = [];

  showRandomMarkers() {
    let randomLat: number, randomLng: number;
    this.positions = [];
    for (let i = 0 ; i < 9; i++) {
      randomLat = Math.random() * 0.0099 + 43.7250;
      randomLng = Math.random() * 0.0099 + -79.7699;
      this.positions.push([randomLat, randomLng]);
    }
  }

  showInfoWindow(marker) {
    marker.ng2MapComponent.openInfoWindow(
      'iw',    // id of InfoWindow
      marker,  // anchor for InfoWindow
      {        // local variables for InfoWindow
        lat: marker.getPosition().lat(),
        lng: marker.getPosition().lng(),
      }
    );
  } 
}

模块文件:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Ng2MapModule } from 'ng2-map';

@NgModule({
  imports: [
    RouterModule.forChild(AlertsRoutes),  
    Ng2MapModule.forRoot({apiUrl: 'https://maps.google.com/maps/api/js?key=mykey'})
  ]
})
export class TestModule { }

另外如何启用图纸管理器?

1 个答案:

答案 0 :(得分:2)

ng2-map给出的示例[1]将'&libraries=visualization,places,drawing'附加到模块导入。我找不到有关可用库的具体文档,但您可能想先尝试所有3,然后看看是否可以删除其中的一些。

它会将您的NgModule代码更改为:

@NgModule({
  imports: [
    RouterModule.forChild(AlertsRoutes),  
    Ng2MapModule.forRoot({apiUrl: 'https://maps.google.com/maps/api/js?key=mykey&libraries=visualization,places,drawing'})
  ]
})

[1] https://github.com/ng2-ui/ng2-map/blob/0e0bd891623fe4adc6a7ea78e76cf1e36456eb8b/app/main.ts