【发布时间】:2018-10-13 17:20:31
【问题描述】:
我想使用 Angular Google Maps 在 Google 地图上放置一个标记,我的坐标来自 JSON 文件,在 Angular 中解析时它变成了一个字符串。
Angular 谷歌地图不支持坐标数字的字符串。所以它需要是数字。
marker.json
[
{
"id":"1",
"lat":"13.751814",
"lon":"100.501060"
},
{
"id":"2",
"lat":"13.738445",
"lon":"100.516805"
},
{
"id":"3",
"lat":"13.730209",
"lon":"100.779991"
}
]
maps.componenet.ts
import { Component, OnInit } from '@angular/core';
import {Http, Response} from '@angular/http';
@Component({
selector: 'app-maps',
templateUrl: './maps.component.html',
styleUrls: ['./maps.component.css']
})
export class MapsComponent implements OnInit {
private data;
constructor(private http:Http){
}
ngOnInit(){
this.getData();
}
getData(){
this.http.get('/localhost/marker.json')
.subscribe(res => this.data = res.json());
}
}
maps.component.html
<agm-map [latitude]="13.359665" [longitude]="101.035913" [zoom]="6">
<ng-container *ngFor="let list of data">
<agm-marker [latitude]="list.lat" [longitude]="list.lon"></agm-marker>
</ng-container>
</agm-map>
我尝试过像这样使用 parseFloat(是的,它不起作用)
<agm-marker [latitude]="parseFloat(list.lat)" [longitude]="parseFloat(list.lon)"></agm-marker>
我正在考虑在 maps.component.ts 文件中使用 parseInt,但我不确定该放在哪里。
请有人帮助我或指导我如何解决这个问题。请让我知道我是否提供了足够的信息。
谢谢!
【问题讨论】:
-
需要手动转换,这是JSON.parse()的默认行为
-
你可以试试
[latitude]="+list.lat"。 -
为什么
parseFloat不起作用?错误? -
@ConnorsFan 谢谢,但它不起作用。
-
@Jeff 它说
parseFloat is not a function(完整的错误日志pastebin.com/8RNSVZ7P)
标签: json angular typescript