我已经定义了一个数组,如下所示,
markers: marker[] = [
{
lat: 51.673858,
lng: 7.815982,
label: 'A',
draggable: true
}
];
我从休息服务获取数据并将新对象推送到标记。
private data: any;
findLocation(): void {
let result;
result = this.geoService.loaddata(this.location)
.subscribe(data => {
result = data;
console.log(result);
this.markers.push({'lat':results[0].geometry.location.lat,'lng':results[0].geometry.location.lng})
});
}
它会抛出错误说文件:
消息:'类型' {' lat':any; }'不能转让给 类型'标记的参数'。物业' lng'缺少类型' { ' lat':任何; }''
结果是
{"结果" :[ { " address_components" :[ { " LONG_NAME" :"科伦坡", " SHORT_NAME" :"科伦坡", "类型" :[" locality"," political" ] }, { " LONG_NAME" :"科伦坡", " SHORT_NAME" :"科伦坡", "类型" :[" administrative_area_level_2","政治" ] }, { " LONG_NAME" :"西部省", " SHORT_NAME" :" WP", "类型" :[" administrative_area_level_1","政治" ] }, { " LONG_NAME" :"斯里兰卡", " SHORT_NAME" :" LK", "类型" :["国家","政治" ] } ] "的formatted_address" :"科伦坡,斯里兰卡", "几何" :{ "边界" :{ "东北" :{ " LAT" :6.9812866, " LNG" :79.8900852 }, "西南" :{ " LAT" :6.862390700000001, " LNG" :79.8223258 } }, "位置" :{ " LAT" :6.9270786, " LNG" :79.861243 }, " LOCATION_TYPE" :" APPROXIMATE", "视" :{ "东北" :{ " LAT" :6.980584400000001, " LNG" :79.8900852 }, "西南" :{ " LAT" :6.8625113, " LNG" :79.8225192 } } }, " place_id" :" ChIJA3B6D9FT4joRjYPTMk0uCzI", "类型" :[" locality"," political" ] }]," status" :"好的" }
答案 0 :(得分:12)
您已将自己的数据订阅到result
,而不是results
。
private data: any;
findLocation(): void {
let result;
// no use for "result" below
// result = this.geoService.loaddata(this.location)
// use the following instead
this.geoService.loaddata(this.location)
.subscribe(data => {
result = data;
console.log(result);
this.markers.push({'lat':results[0].geometry.location.lat,'lng':results[0].geometry.location.lng})
});
}
所以你的推动应该是这样的:
this.markers.push({'lat':result[0].geometry.location.lat,'lng':result[0].geometry.location.lng})
});
但是这也会引发错误,因为在标记中你还声明了label
和draggable
,所以你需要将值推送到这些属性。
答案 1 :(得分:2)
看起来你有一个简单的拼写错误:
this.markers.push({
'lat': results[0].geometry.location.lat,
'lat': results[0].geometry.location.lng
})
应该是:
this.markers.push({
'lat': results[0].geometry.location.lat,
'lng': results[0].geometry.location.lng
})
答案 2 :(得分:2)
首先,你有一个拼写错误,你访问一个名为results
的变量,但只有result
。
其次,结果变量没什么意义。试试这个:
// private data: any; // Remove this field
findLocation(): void {
// no result variable here
this.geoService.loaddata(this.location)
.subscribe(results => { // declare results this way
console.log(results);
this.markers.push({
'lat':results[0].geometry.location.lat,
'lnt':results[0].geometry.location.lng
});
});
}
答案 3 :(得分:1)
this.geoService.loaddata(this.location)
.subscribe(data => {
result = data;
this.markers.push({'lat':data.geometry.location.lat,'lng':data.geometry.location.lng})
});