gpt4 book ai didi

angular - 如何使用 agm-map 和 agm-polyline 将折线的颜色与 Angular 2 交替?

转载 作者:太空狗 更新时间:2023-10-29 17:33:33 24 4
gpt4 key购买 nike

我在我的 Angular 2 车辆跟踪应用程序中使用 agm map 。在这里我使用 agm-map 来显示 map 。我有一个名为 Playback Component 的组件,它表示用户可以查看从特定日期开始行驶的车辆,时间到另一个特定的日期和时间。到目前为止一切正常。在这里我需要提供一个选项以及称为最大速度的日期和时间,这意味着用户可以在 map 中查看车辆在哪些旅行地点上方用户输入了最大速度(比如用户给出的最大速度为 50,而那些单独的行进点应该以红色突出显示)。

我试过以下,

    <agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [mapTypeControl]="true">
<agm-marker [latitude]="latlngMarker.latitude" [longitude]="latlngMarker.longitude">
</agm-marker>
<agm-polyline [strokeColor]="'#2196f3'">
<ng-container *ngFor="let i of latlng">
<agm-polyline-point *ngIf = "i.speed < maxSpeed " [latitude]="i.latitude" [longitude]="i.longitude">
</agm-polyline-point>
</ng-container>
</agm-polyline>
<agm-polyline [strokeColor]="'red'">
<ng-container *ngFor="let i of latlng">
<agm-polyline-point *ngIf = "i.speed > maxSpeed " [latitude]="i.latitude" [longitude]="i.longitude">
</agm-polyline-point>
</ng-container>
</agm-polyline>
</agm-map>

结果如图所示。

enter image description here

但是我想要的东西就像这张图片,

enter image description here

这张图片显示了红色行驶点和蓝色点,其中车辆行驶速度超过了最大速度,我也需要第二张图片中的输出。请帮助我使用 agm map 获得所需的结果聚点。

最佳答案

为了实现这一点,我建议您采用两种逻辑:

1)

第一个逻辑是为数据中的每 2 个点创建折线并根据数据速度属性设置其颜色:

Poyline(array(0), array(1)) , Polyline(array(1), array(2)), ...Poyline(array(i), array(i+1)) 

并检查 array(i) 的 maxSpeed 以设置颜色:

code(我把item改成了pointi换成了index):

<agm-polyline *ngFor="let point of latLng;let i = index;"  [strokeColor]="point.speed < 50 ? '#2196f3': 'red'">
<agm-polyline-point [latitude]="point.latitude" [longitude]="point.longitude">
</agm-polyline-point>
<ng-container *ngIf="polyline[i+1]">
<agm-polyline-point [latitude]="polyline[i+1].latitude" [longitude]="polyline[i+1].longitude">
</agm-polyline-point>
</ng-container>
</agm-polyline>

plunker demonstring 这个:https://embed.plnkr.co/keExxn/

2)

第二个想法 是根据速度将折线的数组分离为多个折线的数组。所以最终的数组看起来像:

let polylines = [
{path: [item1, item2], color: 'red'},
{path: [item3, item4, item5, item6, item7], color: '#2196f3'},
...
]

因此,根据您的主要数据,只需创建一个函数来将数据更改为喜欢这个最终数据。

在 html 中:

  <agm-map [latitude]="latitude" [longitude]="longitude" [scrollwheel]="false" [zoom]="zoom">
<ng-container>
<agm-polyline *ngFor="let polyline of polylines;let i = index;" [strokeColor]="polyline.color">
<agm-polyline-point *ngFor="let point of polyline.path" [latitude]="point.latitude" [longitude]="point.longitude">
</agm-polyline-point>
</agm-polyline>
</ng-container>
</agm-map>

你可以先看看这个plnker:https://embed.plnkr.co/u82rKd/

关于angular - 如何使用 agm-map 和 agm-polyline 将折线的颜色与 Angular 2 交替?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com