Como posso integrar as APIs do Google Maps dentro de um componente Angular 2

tenho um componente Angular 2 que é definido no ficheiro comp.é assim:

import {Component} from 'angular2/core';

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

export class MyComp{
    constructor(){
    }
}

porque quero que o componente mostre um mapa do google, inseri-o no ficheiro comp.html o seguinte código:

<!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> 

este código foi copiado desta ligação http://www.w3schools.com/googleAPI/google_maps_basic.asp o problema é que o componente não mostra o mapa. O que fiz de errado?

Author: nix86, 2016-05-19

3 answers

Sim, podias fazê-lo. Mas haveria um erro no compilador typescript, então não se esqueça de declarar implicitamente a variável google.
declare var google: any;

Aditar esta directiva logo após a importação dos componentes.

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);
  }
}
 21
Author: Ruslan Abramov, 2016-08-07 09:33:57
Encontrei uma solução. Em primeiro lugar, a seguinte linha:
<script src="http://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]"></script>

Deve ser inserido no índice.ficheiro html. O código que cria o mapa deve ser inserido no comp.ts file. Em particular, deve ser inserido num método especial, a saber, "ngOnInit", que pode ser posicionado após o construtor da classe. Este é o 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);
    }
}

Finalmente, o div com id "googleMap", que vai conter o mapa do google, deve ser inserido no comp.ficheiro html, que vai ficar assim:

<body>
    <div id="googleMap" style="width:500px;height:380px;"></div>
</body>
 13
Author: nix86, 2017-11-16 22:17:28

Usar 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 {}
 -4
Author: nisenish, 2016-11-18 01:35:32