2016-08-04 3 views
0

私はtypescriptのプログラミングの初代です。私は、アプリケーションのテンプレートにtypescriptとangular2を使って簡単なGoogleマップを表示しようとしています。 私は、diferentsテンプレートを使って、angle2を使っている簡単なアプリケーションを持っています。私mapping.tsでinstanciateタイプのスクリプトとangular2を持つ単純なGoogleマップ

私が持っている:私の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'; 
 

 
@Component({ 
 
    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> 
 
     
 
    </ul> 
 
    <br> <br> 
 
    <h1>{{title}}</h1> 
 
    <ns-map></ns-map> 
 
    <router-outlet></router-outlet> 
 

 
    `, 
 
    directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES], 
 
    providers: [ 
 
    UserService 
 
    ] 
 
}) 
 

 

 
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() { 
 
    this.getUsers(); 
 
    } 
 
    onSelect(user: User) { this.selecteduser = user; } 
 
}

そして、私のメインのテンプレート内で

//mapping.ts 
 

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

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

 

 

 
@Component({ 
 
    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); 
 
     } 
 
    }

私はjusを持っているTこの

<html> 
 
    <head> 
 
    <title>Capturs</title> 
 
    <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 
 
     src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCCGhv7ryot0iu9ufz-BXlZBZxcYhB7q8c"> 
 
    </script> 
 
    <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> 
 
    <script> 
 
     System.import('app').catch(function(err){ console.error(err); }); 
 
    </script> 
 
    <base href="/" > 
 

 
    </head> 
 
    <!-- 3. Display the application --> 
 
    <body> 
 
    
 
      <my-app>Loading...</my-app> 
 

 

 

 

 
    
 
    </body> 
 
</html>

私はアプリケーションを実行すると、私はマップがありますテンプレートに行くことはできません。これらのエラーメッセージはコンソールに表示されます。

errors messages in console

、あなたは私を助けることができますしてください?

答えて

2

代わりElementの使用ElementRef

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

またはDIVへの参照を取得するViewChildを使用します。

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

@Component({ 
    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.コードを表示するためにスニペットを使用しないでください。

+0

ありがとうございます。私は試して、それは動作します。申し訳ありませんが、私は分かりませんでした。 –

関連する問題