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

时间:2016-05-19 14:39:34

标签: google-maps angular angular2-template

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

import {Component} from 'angular2/core';

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

export class MyComp{
    constructor(){
    }
}

因为我希望组件显示谷歌地图,所以我在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> 

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

3 个答案:

答案 0 :(得分:22)

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

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);
  }
}

答案 1 :(得分:16)

我找到了解决方案。首先,以下一行:

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

必须插入index.html文件中。必须将创建映射的代码插入comp.ts文件中。特别是,它必须插入一个特殊的方法,即“ngOnInit”,它可以放在类的构造函数之后。这是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);
    }
}

最后,必须在comp.html文件中插入ID为“googleMap”且需要包含google地图的div,如下所示:

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

答案 2 :(得分:-5)

使用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 {}