Yseult Fran Yseult Fran - 2 months ago 69x
TypeScript Question

instanciate a simple google map with type script and angular2

i'm a debutant in typescript programmation and i try to display a simple google map using typescript and angular2 on a template of my application.
I have a simple application that using angular2 with rooting with différents templates.

in my mapping.ts i have:


import { Component, Output, EventEmitter } from '@angular/core';

/// <reference path="../typings/googlemaps/google.maps.d.ts" />
/// <reference path="../typings/markerclustererplus/markerclustererplus.d.ts" />

selector: 'ns-map',
template: `
<div id="map"></div>
<button (click)="sayHello()">Say Hello</button>



export class Mapping {

public name: string;
private map: any;
private options: any;

constructor (mapDiv: Element) {
this.name = "GoogleMap";
this.options = {
center: new google.maps.LatLng(53.83305, -1.66412),
zoom: 3,
MapTypeId: google.maps.MapTypeId.TERRAIN
this.map = new google.maps.Map(mapDiv, this.options);

in my app.component.ts

import { Component, OnInit } from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';
import { ROUTER_DIRECTIVES } from '@angular/router';
import {Observable} from 'rxjs/Observable';
import { UserService } from './services/user.service';
import { UsersComponent } from './controllers/users.component';
import { User } from './controllers/user';
import {Mapping} from './mapping';

selector: 'my-app',
template: `
<ul id="tool">
<li style="float:left"><a [routerLink]="['/']"> <img src="logo.png" height="30px" alt=""></a></li>
<li class="toolbar"><a [routerLink]="['/users']">Users </a></li>
<li class="toolbar"><a [routerLink]="['/map']">Map</a></li>
<li class="toolbar"><a [routerLink]="['/form']">Formulaire</a></li>
<li class="toolbar"><a [routerLink]="['/mapp']">Google</a></li> <br>

<br> <br>

providers: [

export class AppComponent {
title = 'Capturs\'s users';
users: User[];
selecteduser: User;

constructor(private userService: UserService) { }
getUsers() {
this.userService.getUsers().then(users => this.users = users);
ngOnInit() {
onSelect(user: User) { this.selecteduser = user; }

And in my main template i have just this

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">

<link rel="icon" type="image/png" href="icone.png" id="icone" />

<script async defer
<script src="https://code.angularjs.org/tools/traceur-runtime.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
System.import('app').catch(function(err){ console.error(err); });
<base href="/" >

<!-- 3. Display the application -->



when I run the application, I can not go to the template where there's the map. I these error messages in the console.

errors messages in console

Please, can you help me?


Use ElementRef instead of Element:

import { ElementRef } from '@angular/core';
constructor (elRef: ElementRef) {
   this.map = new google.maps.Map(elRef.nativeElement, this.options);

Or use ViewChild to get reference to div:

import { Component, ElementRef, ViewChild } from '@angular/core';

  selector: 'ns-map',
  template: `
    <div id="map" #mapDiv></div>
    <button (click)="sayHello()">Say Hello</button> `
export class Mapping  {
  @ViewChild('mapDiv') mapDiv: ElementRef;

  name = "GoogleMap";
  options = {
      center: new google.maps.LatLng(53.83305, -1.66412),
      zoom: 3,
      MapTypeId: google.maps.MapTypeId.TERRAIN
  map: any;

  ngAfterViewInit() {
      this.map = new google.maps.Map(this.mapDiv.nativeElement, this.options);

P.S. please don't use snippet to show your code