JSON.stringify(this.p)
console.log(this.p + " " + typeof(this.p))
这些陈述给了我
[{lat:52.52193980072258,lng:13.401432037353516},{lat:52.52319316685915,lng:13.407096862792969},{lat:52.51969409696076,lng:13.407225608825684}] string
所以this.p
是一个字符串。如果我使用它在我的谷歌地图中绘制一个多边形,它不会给出数组异常(但很自然!)。
如何将其转换为数组(类型可能是google.maps.LatLngLiteral或任何合适的数组)?
注意:如果我执行JSON.stringify(this.p)
:
JSON.parse(this.p)
console.log(this.p + " " + typeof( this.p))
这说:SyntaxError: Unexpected token l in JSON at position 2
出了什么问题以及如何解决?
我在我的多边形中使用它:
var polygon = new google.maps.Polygon({
paths: this.p,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: this.map
});
答案 0 :(得分:0)
完整的方法如下所示:
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;
}
)