Google maps 使用类型script和angular2实例化一个简单的google地图
我是typescript编程的新手,我尝试在我的应用程序模板上使用typescript和angular2显示一个简单的google地图。 我有一个简单的应用程序,它使用angular2和différents模板进行生根 在my mapping.ts中,我有:Google maps 使用类型script和angular2实例化一个简单的google地图,google-maps,typescript,angular,angular2-routing,Google Maps,Typescript,Angular,Angular2 Routing,我是typescript编程的新手,我尝试在我的应用程序模板上使用typescript和angular2显示一个简单的google地图。 我有一个简单的应用程序,它使用angular2和différents模板进行生根 在my mapping.ts中,我有: //mapping.ts 从“@angular/core”导入{Component,Output,EventEmitter}; /// /// @组成部分({ 选择器:“ns映射”, 模板:` 打招呼 ` }) 导出类映射{ 公共名称
//mapping.ts
从“@angular/core”导入{Component,Output,EventEmitter};
///
///
@组成部分({
选择器:“ns映射”,
模板:`
打招呼
`
})
导出类映射{
公共名称:字符串;
私人地图:任何;
私人选择:任何;
构造函数(mapDiv:Element){
this.name=“谷歌地图”;
此选项={
中心:新google.maps.LatLng(53.83305,-1.66412),
缩放:3,
MapTypeId:google.maps.MapTypeId.TERRAIN
};
this.map=new google.maps.map(mapDiv,this.options);
}
}
使用ElementRef
而不是Element
:
import { ElementRef } from '@angular/core';
...
constructor (elRef: ElementRef) {
...
this.map = new google.maps.Map(elRef.nativeElement, this.options);
}
或使用ViewChild获取对div的引用:
import { Component, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'ns-map',
template: `
<div id="map" #mapDiv></div>
<button (click)="sayHello()">Say Hello</button> `
})
export class Mapping {
@ViewChild('mapDiv') mapDiv: ElementRef;
name = "GoogleMap";
options = {
center: new google.maps.LatLng(53.83305, -1.66412),
zoom: 3,
MapTypeId: google.maps.MapTypeId.TERRAIN
};
map: any;
ngAfterViewInit() {
this.map = new google.maps.Map(this.mapDiv.nativeElement, this.options);
}
}
从'@angular/core'导入{Component,ElementRef,ViewChild};
@组成部分({
选择器:“ns映射”,
模板:`
打招呼`
})
导出类映射{
@ViewChild('mapDiv')mapDiv:ElementRef;
name=“谷歌地图”;
选项={
中心:新google.maps.LatLng(53.83305,-1.66412),
缩放:3,
MapTypeId:google.maps.MapTypeId.TERRAIN
};
地图:任何;
ngAfterViewInit(){
this.map=new google.maps.map(this.mapDiv.nativeElement,this.options);
}
}
p.S.请不要使用代码片段显示您的代码使用
ElementRef
而不是Element
:
import { ElementRef } from '@angular/core';
...
constructor (elRef: ElementRef) {
...
this.map = new google.maps.Map(elRef.nativeElement, this.options);
}
或使用ViewChild获取对div的引用:
import { Component, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'ns-map',
template: `
<div id="map" #mapDiv></div>
<button (click)="sayHello()">Say Hello</button> `
})
export class Mapping {
@ViewChild('mapDiv') mapDiv: ElementRef;
name = "GoogleMap";
options = {
center: new google.maps.LatLng(53.83305, -1.66412),
zoom: 3,
MapTypeId: google.maps.MapTypeId.TERRAIN
};
map: any;
ngAfterViewInit() {
this.map = new google.maps.Map(this.mapDiv.nativeElement, this.options);
}
}
从'@angular/core'导入{Component,ElementRef,ViewChild};
@组成部分({
选择器:“ns映射”,
模板:`
打招呼`
})
导出类映射{
@ViewChild('mapDiv')mapDiv:ElementRef;
name=“谷歌地图”;
选项={
中心:新google.maps.LatLng(53.83305,-1.66412),
缩放:3,
MapTypeId:google.maps.MapTypeId.TERRAIN
};
地图:任何;
ngAfterViewInit(){
this.map=new google.maps.map(this.mapDiv.nativeElement,this.options);
}
}
p.S.请不要使用代码片段显示您的代码谢谢您的帮助。我试过了,效果很好。很抱歉,我不知道代码片段。您好,我得到的消息是,类型GlobalEventHandlers上不存在mapDiv错误。谢谢您的帮助。我试过了,效果很好。抱歉,我不知道代码片段。您好,我得到的错误是,类型GlobalEventHandlers上不存在mapDiv