2017-11-02 4 views
0

저는 TypeScript를 배우고 있습니다. 내 위치를 얻고 Googlemap API를 사용하여 거리와 도시에 대한 정보를 얻고 싶을 때 해결할 수없는 문제가 있습니다. 항상 말했습니다. 그 재산의 'str'을 읽을 수 없다 ... 나는 왜 그런지 모르겠다. 누군가 나를 도울 수 있습니까?null 형식의 'str'속성을 읽을 수 없습니다.

import { Component, OnInit } from '@angular/core'; 
import {Http, Response} from '@angular/http'; 
import 'rxjs/Rx'; 
import {Observable} from 'rxjs/'; 

@Component({ 
    selector: 'app-center', 
    templateUrl: './center.component.html', 
    styleUrls: ['./center.component.css'] 
}) 
export class CenterComponent implements OnInit { 
    dataSource: Observable<any>; 
    locationInfo: any; 
    str: any; 
    constructor(public http: Http) { 
    this.dataSource = this.http.get('https://maps.googleapis.com/maps/api/geocode/json?' 
     + 'latlng=' + this.str + '&key=AIzaSyC2wdo6kMAXpuhwuUa8fM4mCkDy_kZeWKY').map(Response => Response.json()); 
    } 
    ngOnInit() { 
    } 
    getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(this.showPostion, this.showError); 
    } 
    } 
    showPostion(position) { 
    const latitude = position.coords.latitude; 
    const longitude = position.coords.longitude; 
    this.str.bind(this); 
    this.str = String(latitude + ',' + longitude); 
    console.log(this.str); 
    } 
    showError() { 
    console.log('Invalid Address !'); 
    } 
    getData() { 
    this.dataSource.subscribe(
     data => this.locationInfo = data 
    ); 
    console.log(this.locationInfo); 
    } 
} 
+0

당신의 스택 트레이스를 제공 할 수 바인딩해야 문제가 어디에서 발생 했습니까? – JamesFaix

답변

0

당신은 현재 클래스에 콜백을 (TS 당신을 위해이 작업을 수행하지 않습니다) 가 this.str.bind(this); (바인드는 기능입니다) 제거 navigator.geolocation.getCurrentPosition(this.showPostion.bind(this), this.showError.bind(this)); 루게릭 병을 시도

+0

str을 전역으로 만들어서 java.How와 같은 다른 함수에서 사용할 수 있도록하고 싶습니다. –

+0

전체 코드 구조, 전체 컴파일 방법 및 전체적으로 필요한 부분에 완전히 의존합니다. 상황에 닮은 것을 찾을 수 있는지 알아보기 위해 Google에 TypeScript와 전역 변수에 대한 정보를 제공하는 것이 좋습니다. –

+0

정말 고맙습니다. 고맙습니다! –

관련 문제