|
| 1 | +import { Component, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core'; |
| 2 | +import { Feature as OlFeature, layer as OlLayer, style } from 'openlayers'; |
| 3 | +import { MapComponent, LayerVectorComponent } from 'ngx-openlayers'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'app-color-select-hover', |
| 7 | + template: ` |
| 8 | + <aol-map #map (onPointerMove)="changeFeatureHovered($event)" width="100%" height="100%"> |
| 9 | + <aol-interaction-default></aol-interaction-default> |
| 10 | + <aol-control-defaults></aol-control-defaults> |
| 11 | + <aol-interaction-select #select [style]="styleInterationSelected"></aol-interaction-select> |
| 12 | +
|
| 13 | +
|
| 14 | + <aol-view [zoom]="5"> |
| 15 | + <aol-coordinate [x]="1.4886" [y]="43.5554" [srid]="'EPSG:4326'"></aol-coordinate> |
| 16 | + </aol-view> |
| 17 | +
|
| 18 | + <aol-layer-tile [opacity]="1"> |
| 19 | + <aol-source-osm></aol-source-osm> |
| 20 | + </aol-layer-tile> |
| 21 | +
|
| 22 | + <aol-layer-group> |
| 23 | + <aol-layer-vector #aoiLayerVector *ngFor="let f of features.features"> |
| 24 | + <aol-style *ngIf="f.id === hoveredFeatureId; else notHovered"> |
| 25 | + <aol-style-stroke [color]="'white'" width="3"></aol-style-stroke> |
| 26 | + <aol-style-fill [color]="'rgba(90, 17, 26, 0.3)'" ></aol-style-fill> |
| 27 | + </aol-style> |
| 28 | + <ng-template #notHovered> |
| 29 | + <aol-style> |
| 30 | + <aol-style-stroke [color]="'rgba(90, 17, 26)'" width="3"></aol-style-stroke> |
| 31 | + <aol-style-fill [color]="'rgba(90, 17, 26, 0.5)'" ></aol-style-fill> |
| 32 | + </aol-style> |
| 33 | + </ng-template> |
| 34 | + <aol-source-vector> |
| 35 | + <aol-feature [id]="f.id"> |
| 36 | + <aol-geometry-polygon> |
| 37 | + <aol-collection-coordinates [coordinates]="f.geometry.coordinates[0]" [srid]="'EPSG:4326'" ></aol-collection-coordinates> |
| 38 | + </aol-geometry-polygon> |
| 39 | + </aol-feature> |
| 40 | + </aol-source-vector> |
| 41 | + </aol-layer-vector> |
| 42 | + </aol-layer-group> |
| 43 | + </aol-map> |
| 44 | + `, |
| 45 | +}) |
| 46 | +export class ColorSelectHoverComponent implements OnInit { |
| 47 | + constructor() {} |
| 48 | + |
| 49 | + @ViewChild('map') map: MapComponent; |
| 50 | + @ViewChildren('aoiLayerVector') aoiLayerVector: QueryList<LayerVectorComponent>; |
| 51 | + |
| 52 | + features = { |
| 53 | + type: 'FeatureCollection', |
| 54 | + features: [ |
| 55 | + { |
| 56 | + id: 1, |
| 57 | + type: 'Feature', |
| 58 | + properties: {}, |
| 59 | + geometry: { |
| 60 | + type: 'Polygon', |
| 61 | + coordinates: [ |
| 62 | + [ |
| 63 | + [-1.4501953125, 48.40003249610685], |
| 64 | + [2.13134765625, 48.40003249610685], |
| 65 | + [2.13134765625, 50.13466432216694], |
| 66 | + [-1.4501953125, 50.13466432216694], |
| 67 | + [-1.4501953125, 48.40003249610685], |
| 68 | + ], |
| 69 | + ], |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + id: 2, |
| 74 | + type: 'Feature', |
| 75 | + properties: {}, |
| 76 | + geometry: { |
| 77 | + type: 'Polygon', |
| 78 | + coordinates: [ |
| 79 | + [ |
| 80 | + [5.3173828125, 47.368594345213374], |
| 81 | + [9.29443359375, 47.368594345213374], |
| 82 | + [9.29443359375, 49.36806633482156], |
| 83 | + [5.3173828125, 49.36806633482156], |
| 84 | + [5.3173828125, 47.368594345213374], |
| 85 | + ], |
| 86 | + ], |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + id: 3, |
| 91 | + type: 'Feature', |
| 92 | + properties: {}, |
| 93 | + geometry: { |
| 94 | + type: 'Polygon', |
| 95 | + coordinates: [ |
| 96 | + [ |
| 97 | + [-3.3837890625, 43.61221676817573], |
| 98 | + [1.51611328125, 43.61221676817573], |
| 99 | + [1.51611328125, 46.694667307773116], |
| 100 | + [-3.3837890625, 46.694667307773116], |
| 101 | + [-3.3837890625, 43.61221676817573], |
| 102 | + ], |
| 103 | + ], |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + id: 4, |
| 108 | + type: 'Feature', |
| 109 | + properties: {}, |
| 110 | + geometry: { |
| 111 | + type: 'Polygon', |
| 112 | + coordinates: [ |
| 113 | + [ |
| 114 | + [4.50439453125, 42.342305278572816], |
| 115 | + [9.16259765625, 42.342305278572816], |
| 116 | + [9.16259765625, 45.66012730272194], |
| 117 | + [4.50439453125, 45.66012730272194], |
| 118 | + [4.50439453125, 42.342305278572816], |
| 119 | + ], |
| 120 | + ], |
| 121 | + }, |
| 122 | + }, |
| 123 | + ], |
| 124 | + }; |
| 125 | + |
| 126 | + styleInterationSelected = new style.Style({ |
| 127 | + fill: new style.Fill({ |
| 128 | + color: 'rgba(0, 153, 255, 0.1)', |
| 129 | + }), |
| 130 | + stroke: new style.Stroke({ |
| 131 | + color: 'rgba(0, 153, 255)', |
| 132 | + width: 3, |
| 133 | + }), |
| 134 | + }); |
| 135 | + |
| 136 | + hoveredFeatureId; |
| 137 | + |
| 138 | + ngOnInit() {} |
| 139 | + |
| 140 | + changeFeatureHovered(event) { |
| 141 | + const hit: OlFeature = this.map.instance.forEachFeatureAtPixel(event.pixel, f => f, { |
| 142 | + layerFilter: inLayer(...this.aoiLayerVector.toArray()), |
| 143 | + hitTolerance: 10, |
| 144 | + }) as OlFeature; |
| 145 | + |
| 146 | + if (!hit && this.hoveredFeatureId) { |
| 147 | + this.hoveredFeatureId = null; |
| 148 | + } |
| 149 | + if (hit && hit.getId() !== this.hoveredFeatureId) { |
| 150 | + this.hoveredFeatureId = hit.getId(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +function inLayer(...layers: LayerVectorComponent[]): (l: OlLayer.Layer) => boolean { |
| 156 | + return (l: OlLayer.Layer) => layers.some(layer => layer.instance === l); |
| 157 | +} |
0 commit comments