gpt4 book ai didi

javascript - Angular2 - 谷歌地图 -drawingManager 未定义

转载 作者:行者123 更新时间:2023-12-01 03:57:28 25 4
gpt4 key购买 nike

您好,我正在尝试启用谷歌地图中的绘图工具。但出现如下错误:

VM1935 zone.js@0.6.25?main=browser:140 未捕获类型错误:无法读取未定义的属性“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 { }

另外,如何启用绘图管理器?

最佳答案

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

关于javascript - Angular2 - 谷歌地图 -drawingManager 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42525986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com