angular - Angular Google Maps 航路点不与动态航路点数组一起显示
问题描述
我正在尝试在 Angular Google Maps 中的起点和终点地址之间获取航点。我有一个数组
waypoints: any
和一个动态添加航点的方法:
addWaypoint($event) {
this.waypoints.push({
location: { lat: $event.geoLocation.latitude, lng: $event.geoLocation.longitude},
stopover: true
});
console.log(this.waypoints);
}
但这些航点不会显示在地图上。
<agm-map [latitude]="lat" [longitude]="lng" [scrollwheel]="false" [zoom]="zoom">
<agm-marker *ngIf="!destination" [latitude]="lat" [longitude]="lng"></agm-marker>
<agm-direction *ngIf="destination" [origin]="origin" [destination]="destination" [waypoints]="waypoints">
</agm-direction>
</agm-map>
如果我将航点静态添加到数组中,它们将显示:
addWaypoint($event) {
this.waypoints = [{
location: { lat: $event.geoLocation.latitude, lng: $event.geoLocation.longitude},
stopover: true
}];
console.log(this.waypoints);
}
控制台输出在两种情况下都是相同的:
有什么建议么?谢谢
解决方案
通过使用扩展运算符找到了解决方案:
this.waypoints = [...this.waypoints, ...[{
location: { lat: $event.geoLocation.latitude, lng: $event.geoLocation.longitude},
stopover: true
}]];
推荐阅读
- amazon-web-services - 防止开发人员启动过多或过大的 AWS 实例的机制?
- java - 使用批量更新和线程在 for 循环中更新查询
- javascript - react-pdf/renderer - 图像显示在 PDF 预览中,但下载后不在 PDF 文件中
- javascript - 更新时无法在 Mongoose 的 updateOne 挂钩中设置新值
- javascript - jQuery.html 返回子元素,而不是选定的
- javascript - 我可以让 VSCode 上的 Code Runner 扩展与 ES6 一起使用吗?
- amazon-web-services - 如何在 terraform 中使用新引入的 aws_cloudfront_cache_policy 资源
- .net - 如何获取解析到命令行应用程序的参数?
- angular - 将焦点集中在模态中,使 Angular 10 中的 ADA 兼容
- c# - 如何使用 Hot Chocolate 以 Code First 方法将字段指向另一个微服务