【问题标题】:How to alternate color of polyline with angular 2 using agm-map & agm-polyline?如何使用 agm-map 和 agm-polyline 将折线的颜色与角度 2 交替?
【发布时间】:2018-03-27 13:37:30
【问题描述】:

我在我的 Angular 2 车辆跟踪应用程序中使用 agm 地图。这里我使用 agm-map 来显示地图。我有一个名为 Playback Component 的组件,它表示,用户可以查看从特定日期行驶的车辆和时间到另一个特定的日期和时间。到目前为止一切正常。在这里我需要提供一个选项以及日期和时间,称为最大速度,这意味着用户可以在地图中查看车辆经过的旅行地点用户输入了最大速度(假设用户将最大速度设置为 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>

结果和图片一样。

但我想要的是这样的图片,

这张图片显示了红色的旅行点和蓝色的颜色点,其中车辆的行驶速度超过了最大速度,我也需要像第二张图片中一样的输出。请帮助我使用 agm map 实现所需的结果多边形。

【问题讨论】:

    标签: angular google-maps typescript google-maps-api-3 google-maps-markers


    【解决方案1】:

    要实现这一点,我建议你 2 个逻辑:

    1)

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

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

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

    代码(我将项目更改为point,将i 更改为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 演示这个: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/

    【讨论】:

    • 嗨,我有一个这样的编码折线 'sjvA{w_jSo@y@bEwAy@wB^y@{AqEHqBuA}CZgBp@cb@j@iAYo@`Lui@r@OHs@qB}@cNgVy@qDTaAsJaJfDoCkAsCfAk@q]_k@ on@qk@eBnBgC{C',是否可以在不解码为点数组的情况下将其显示在 agm-map 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2019-10-15
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多