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?
3 answers
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);
}
}
<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>
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 {}