2017-07-11 258 views
-1

我想用標記填充Google地圖。將「字符串」值轉換爲數字

標記座標,我得到的字符串:

"marker": { 
     "latitude": "52.31976", 
     "longitude": "7.00454" 
} 

我對檢索JSON服務:

/** 
    * Get All Sources 
    */ 
    getSources(): Observable<Source.RootObject[]> { 
     return this._http.get(this._sourceUrl) 
      .map((response: Response) => <Source.RootObject[]>response.json()); 
    } 

我的部件處理呼叫:

/** 
    * Set local _sources array equal data from stream 
    * @param _sourceService 
    * @param router 
    */ 
    constructor(
     private _sourceService: SourceService, 
     private router: Router 
    ) { 
     this._sourceService.getSources() 
      .subscribe(_sources => this.createSources(_sources)); 
    } 

的createMArkers方法:

/** 
    * Bad workaround for markers 
    */ 
    private createSources(result): void { 
     this._sources = result; 

     let sourceLat: number, sourceLng: number; 
     this.positions = []; 
     let latlong = null; 

     for (let i = 0; i < this._sources.length; i++) { 

      sourceLat = +this._sources[i].marker.latitude; 
      sourceLng = +this._sources[i].marker.longitude; 
      // Create position object 
      latlong = { lat: sourceLat, lng: sourceLng }; 

      this.positions.push([this._sources[i].id, latlong]); 
     } 
    } 

的HTML標記與角:

<ngui-map zoom="8" center="{{center}}" > 

    <marker *ngFor="let pos of positions" 
       [icon]="{ 
       url: 'assets/icons/marker.svg', 
       anchor: [16,16], 
       size: [32,32], 
       scaledSize: [32,32] 
      }" 
      (click)="printLog(pos)" 
      [position]="pos.latlong"></marker> 
    </ngui-map> 

我不希望創建的位置的單獨的陣列,但只是使用源對象的陣列(例如source.marker.longitude)

我試了一下:

  • [position]="marker.latitude, marker.longitude"是不能接受的,因爲它是一個字符串值。
  • [position]=marker.latitude, marker.longitude希望它會被鑄造出來。
  • [position]={{marker}}

在此先感謝。

+0

@toskv的與打字稿,在component.ts,但如何與角度解決這個問題? – Lobato

+1

您可以在處理來自服務器的響應的同時,使用JavaScript進行轉換。 – toskv

+0

你能指點我在正確的方向..? – Lobato

回答

0

嘗試

[position]="+marker.latitude, +marker.longitude" 

基本JS轉換

+0

解析器錯誤:[+標記中第24列的意外標記','。緯度,+ marker.longitude] [錯誤 - >] [位置] =「+ marker.latitude,+ marker.longitude」 – Lobato

+0

這是你用'[position] =「marker.latitude,marker.longitude」你沒有得到這個錯誤,或者你在你的文章中犯了錯誤! – trichetriche

+0

同樣的錯誤,你到底意味着什麼? – Lobato