2017-04-25 52 views
0

我想根據服務的位置信息顯示位置圖。我得到的位置信息correctly.But我得到html.Where一個未定義的錯誤做我犯錯誤無法導航:無法讀取undefined.ERROR的屬性'lng'錯誤:未捕獲(承諾):TypeError:無法讀取未定義的屬性'lng'

Map.html

<ion-header> 

<ion-navbar> 
<ion-title>Map</ion-title> 
</ion-navbar> 

</ion-header> 


    <ion-content> 
     <sebm-google-map id="map" [latitude]="map.lat" [longitude]="map.lng" 
     [zoom]="map.zoom"> 
     </sebm-google-map> 
</ion-content> 

map.ts

import { Component } from '@angular/core'; 
import { IonicPage, NavController, NavParams } from 'ionic-angular'; 
import {AgmCoreModule,SebmGoogleMap} from 'angular2-google-maps/core'; 
import {DataApi} from '../../app/shared/shared'; 

@IonicPage() 
@Component({ 
selector: 'page-map', 
templateUrl: 'map.html', 


}) 
export class MapPage { 

    map : any; 
    constructor(public navCtrl: NavController, public navParams: 
    NavParams,private dataApi : DataApi) { 
    } 

    ionViewDidLoad() { 
    console.log('ionViewDidLoad MapPage'); 
    let games = this.navParams.data; 
    let tourneyData = this.dataApi.getCurrentTourney(); 
    let location = tourneyData.locations[games.locationId]; 

    this.map = { 
    lat : location.latitude, 
    lng : location.longitude, 
    zoom : 60, 
    markerLabel : games.location 
    }; 
    console.log(this.map.lat); 
    console.log(this.map.lng); 
    console.log(this.map.markerLabel); 
} 

} 

數據api.service.ts

import {Injectable} from '@angular/core'; 
import {Http,Response} from '@angular/http'; 

import 'rxjs'; 
import {Observable} from 'rxjs/Observable'; 
@Injectable() 
export class DataApi { 

private url = 'https://ionic2-9dc0a.firebaseio.com'; // https://ionic2-9dc0a.firebaseio.com 
currentTourney : any = {}; 
private tourneyData = {}; 
constructor(private http:Http){ 
} 
    getTournaments(){ 
    return new Promise(resolve =>{ 
     this.http.get(`${this.url}/tournaments.json`) 
     .subscribe(res => resolve(res.json())) 
    }); 
    } 


/* getAdress(): Promise<Team> { 
    return this.http.get(this.url) 
    .map(rsp => rsp.json()) 
    .toPromise(); 
} 
*/  

    getTournamentData(tourneyId,forceRefresh : boolean = false) : Observable<any>{ 

    if(!forceRefresh && this.tourneyData[tourneyId]){ 
     this.currentTourney = this.tourneyData[tourneyId]; 
     console.log("no need to make Http call"); 
     return Observable.of(this.currentTourney); 

    } 
    console.log("about to make Http call") 
    return this.http.get(`${this.url}/tournaments-data/${tourneyId}.json`) 
    .map((response:Response) =>{ 
     this.currentTourney = response.json(); 
     return this.currentTourney; 
    }); 
    } 


    getCurrentTourney(){ 
    return this.currentTourney; 
    } 

    refreshCurrentTourney(){ 
    return this.getTournamentData(this.currentTourney.tournament.id,true); 
    } 

} 

回答

1

要設置在ionViewDidLoadthis.map數據被稱爲一次頁面loaded.Check here

當模板被加載時,您的地圖可能不會被設置。只需使用安全導航運營商?

<ion-content> 
     <sebm-google-map id="map" [latitude]="map?.lat" [longitude]="map?.lng" 
     [zoom]="map?.zoom"> 
     </sebm-google-map> 
</ion-content> 
+0

你真的很棒。在上週幫助我兩次失誤。我非常感激。 –

相關問題