如何将 Google Maps API 集成到 Angular 2 组件中

How can I integrate Google Maps APIs inside an Angular 2 component

我有一个 Angular 2 组件,它在文件 comp.ts 中这样定义:

import {Component} from 'angular2/core';

@component({
    selector:'my-comp',
    templateUrl:'comp.html'
})

export class MyComp{
    constructor(){
    }
}

因为我想让组件显示 google 地图,所以我在文件 comp.html 中插入了以下代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>

</html> 

此代码已从 link http://www.w3schools.com/googleAPI/google_maps_basic.asp 中复制。问题是组件不显示地图。我做错了什么?

我找到了解决办法。首先,下面一行:

<script src="http://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]"></script>

必须插入到 index.html 文件中。创建地图的代码必须插入到 comp.ts 文件中。特别是,它必须插入一个特殊的方法中,即"ngOnInit",可以将其定位在class的构造函数之后。这是 comp.ts:

import { Component } from 'angular2/core';

declare const google: any;

@Component({
    selector: 'my-app',
    templateUrl:'appHtml/app.html',
    styleUrls: ['css/app.css']
})

export class AppMainComponent {
    constructor() {}
    ngOnInit() {
        let mapProp = {
            center: new google.maps.LatLng(51.508742, -0.120850),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        let map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    }
}

最后,id 为 "googleMap" 的 div 将包含 google 地图,必须插入到 comp.html 文件中,它看起来像这个:

<body>
    <div id="googleMap" style="width:500px;height:380px;"></div>
</body>

是的,你可以这样做。但是打字稿编译器会出错,所以不要忘记隐式声明google变量。

declare var google: any;

在组件导入后立即添加此指令。

import { Component, OnInit } from '@angular/core';
declare var google: any;

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent implements OnInit {
  ngOnInit() {
    var mapProp = {
            center: new google.maps.LatLng(51.508742, -0.120850),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
      var map = new google.maps.Map(document.getElementById("gmap"), mapProp);
  }
}

使用 angular2-google-maps: https://angular-maps.com/

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';

import { AgmCoreModule } from 'angular2-google-maps/core';

@Component({
  selector: 'app-root',
    styles: [`
        .sebm-google-map-container {
            height: 300px;
        }
    `],
  template: `
        <sebm-google-map [latitude]="lat" [longitude]="lng"></sebm-google-map>
    `
})
export class AppComponent {
  lat: number = 51.678418;
  lng: number = 7.809007;
}

@NgModule({
  imports: [
    BrowserModule,
    AgmCoreModule.forRoot({
      apiKey: 'YOUR_GOOGLE_MAPS_API_KEY'
    })
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}