[英]angular2/ typescript convert string into an array
JSON.stringify(this.p)
console.log(this.p + " " + typeof(this.p))
These statements give me 这些陈述给我
[{lat:52.52193980072258,lng:13.401432037353516},{lat:52.52319316685915,lng:13.407096862792969},{lat:52.51969409696076,lng:13.407225608825684}] string
So this.p
is a string. 所以
this.p
是一个字符串。 If I use it to draw a polygon in my google map, it gives not an Array exception (but natural!). 如果我用它在我的Google地图上绘制一个多边形,它不会给出Array异常(而是自然的!)。
How can I convert it to an array (of type perhaps google.maps.LatLngLiteral or any suitable one)? 如何将其转换为数组(类型可能是google.maps.LatLngLiteral或任何合适的数组)?
Note : If I do after JSON.stringify(this.p)
: 注意 :如果我在
JSON.stringify(this.p)
之后JSON.stringify(this.p)
:
JSON.parse(this.p)
console.log(this.p + " " + typeof( this.p))
this says: SyntaxError: Unexpected token l in JSON at position 2
这说:
SyntaxError: Unexpected token l in JSON at position 2
What's going wrong and how can I fix it? 怎么了,我该如何解决?
I use it like this in my polygon: 我在多边形中像这样使用它:
var polygon = new google.maps.Polygon({
paths: this.p,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: this.map
});
The complete method looks like following: 完整的方法如下所示:
var myVar=[]
this.exhibitionSurveyObjectService.getObjects()
.subscribe(
response => {
this.exhibitionSurveyObjects = response;
console.log(this.exhibitionSurveyObjects)
for(var i = 0; i < this.exhibitionSurveyObjects.length; i++){
myVar.push(JSON.parse(this.exhibitionSurveyObjects[i].path))
}
var polygon = new google.maps.Polygon({
paths: myVar,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: this.map
});
},
error => {
console.log("error")
this.errorMessage = <any>error;
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.