|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { LngLatBounds } from 'mapbox-gl'; |
| 3 | + |
| 4 | +@Component({ |
| 5 | + template: ` |
| 6 | + <mgl-map |
| 7 | + style="mapbox://styles/mapbox/light-v9" |
| 8 | + [zoom]="[12]" |
| 9 | + [center]="[-77.0214, 38.8970]" |
| 10 | + [fitBounds]="bounds" |
| 11 | + [fitBoundsOptions]="{ |
| 12 | + padding: 20 |
| 13 | + }" |
| 14 | + > |
| 15 | + <mgl-control> |
| 16 | + <button |
| 17 | + mat-raised-button |
| 18 | + (click)="zoomToBounds()" |
| 19 | + > |
| 20 | + Zoom to bounds |
| 21 | + </button> |
| 22 | + </mgl-control> |
| 23 | + <mgl-layer |
| 24 | + id="LineString" |
| 25 | + type="line" |
| 26 | + [source]="source" |
| 27 | + [paint]="{ |
| 28 | + 'line-color': '#BF93E4', |
| 29 | + 'line-width': 5 |
| 30 | + }" |
| 31 | + [layout]="{ |
| 32 | + 'line-join': 'round', |
| 33 | + 'line-cap': 'round' |
| 34 | + }" |
| 35 | + ></mgl-layer> |
| 36 | + </mgl-map> |
| 37 | + `, |
| 38 | + styleUrls: ['./examples.css'], |
| 39 | + preserveWhitespaces: false |
| 40 | +}) |
| 41 | +export class ZoomtoLinestringComponent { |
| 42 | + bounds: LngLatBounds; |
| 43 | + |
| 44 | + source = { |
| 45 | + type: 'geojson', |
| 46 | + data: { |
| 47 | + 'type': 'FeatureCollection', |
| 48 | + 'features': [{ |
| 49 | + 'type': 'Feature', |
| 50 | + 'geometry': { |
| 51 | + 'type': 'LineString', |
| 52 | + 'properties': {}, |
| 53 | + 'coordinates': [ |
| 54 | + [-77.0366048812866, 38.89873175227713], |
| 55 | + [-77.03364372253417, 38.89876515143842], |
| 56 | + [-77.03364372253417, 38.89549195896866], |
| 57 | + [-77.02982425689697, 38.89549195896866], |
| 58 | + [-77.02400922775269, 38.89387200688839], |
| 59 | + [-77.01519012451172, 38.891416957534204], |
| 60 | + [-77.01521158218382, 38.892068305429156], |
| 61 | + [-77.00813055038452, 38.892051604275686], |
| 62 | + [-77.00832366943358, 38.89143365883688], |
| 63 | + [-77.00818419456482, 38.89082405874451], |
| 64 | + [-77.00815200805664, 38.88989712255097] |
| 65 | + ] |
| 66 | + } |
| 67 | + }] |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + zoomToBounds() { |
| 72 | + const coordinates = this.source.data.features[0].geometry.coordinates; |
| 73 | + |
| 74 | + this.bounds = coordinates.reduce((bounds, coord) => { |
| 75 | + return bounds.extend(<any>coord); |
| 76 | + }, new LngLatBounds(coordinates[0], coordinates[0])); |
| 77 | + } |
| 78 | +} |
0 commit comments