【问题标题】:How can I integrate Google Maps APIs inside an Angular 2 component如何在 Angular 2 组件中集成 Google Maps API
【发布时间】:2016-09-16 12:45:25
【问题描述】:

我有一个 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 复制。问题是组件不显示地图。我做错了什么?

【问题讨论】:

    标签: google-maps angular angular2-template


    【解决方案1】:

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

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

    最后,id 为“googleMap”的 div 将包含谷歌地图,必须插入到 comp.html 文件中,如下所示:

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

    【讨论】:

    • 您应该将您希望地图显示在其中的元素放入组件的 html 模板中。只是为了澄清。您的模板中通常没有正文标签。
    • 这种解决方案也适用于 Angular 5.x。很好的答案,谢谢!如果你在那个版本中使用它,你可能需要npm install @types/googlemaps --save
    • @tkhm 我在控制台中收到google is not defined
    • 你导入类型了吗?换句话说,你有没有在你的 ts 文件中添加 import { } from @types/googlemaps
    【解决方案2】:

    是的,你可以这样做。但是typescript编译器会出错,所以不要忘记隐式声明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);
      }
    }
    

    【讨论】:

    • ngOnInit 帮助我解决了 getElementById 问题,我非常感谢!从 google var 声明开始,我建议使用 [gmaps typings] (github.com/DefinitelyTyped/DefinitelyTyped/blob/master/…) 它真的很有帮助
    • @youssef 我在控制台中得到google is not defined
    • @Arbaaz 我猜你的应用程序中没有正确导入谷歌地图 JS,你能提供你的应用程序的 GIST/简单代码吗?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 2012-09-26
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多