2017-07-11 262 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