|
| 1 | +import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, NO_ERRORS_SCHEMA, NgZone, OnDestroy, OnInit, ViewChild, inject } from '@angular/core'; |
| 2 | +import { ActionBarComponent, NavigationButtonDirective, RouterExtensions, registerElement } from '@nativescript/angular'; |
| 3 | +import { MapStyle, MapboxApi, MapboxMarker, MapboxTraceCategory, MapboxView } from '@nativescript-community/ui-mapbox'; |
| 4 | +import { Trace } from '@nativescript/core'; |
| 5 | +import { MapboxHeaderComponent } from '../mapbox-header/mapbox-header.component'; |
| 6 | +import { MAPBOX_ACCESS_TOKEN } from '../mapbox-token'; |
| 7 | +import { MapboxFooterComponent } from '../mapbox-footer/mapbox-footer.component'; |
| 8 | + |
| 9 | +registerElement('Mapbox', () => require('@nativescript-community/ui-mapbox').MapboxView); |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'app-mapbox-demo2', |
| 13 | + templateUrl: './mapbox-demo2.component.html', |
| 14 | + schemas: [NO_ERRORS_SCHEMA], |
| 15 | + imports: [ActionBarComponent, NavigationButtonDirective, MapboxHeaderComponent, MapboxFooterComponent] |
| 16 | +}) |
| 17 | +export class MapboxDemo2Component implements OnInit, AfterViewInit, OnDestroy { |
| 18 | + router: RouterExtensions = inject(RouterExtensions); |
| 19 | + cd: ChangeDetectorRef = inject(ChangeDetectorRef); |
| 20 | + ngZone: NgZone = inject(NgZone); |
| 21 | + |
| 22 | + @ViewChild('mapContainer', { static: false }) public mapContainerRef: ElementRef; |
| 23 | + |
| 24 | + mapboxView: MapboxView; |
| 25 | + mapbox: MapboxApi; |
| 26 | + |
| 27 | + markers: MapboxMarker[] = []; |
| 28 | + |
| 29 | + ngOnInit() { |
| 30 | + console.log('----- MapboxDemo2Component initialized'); |
| 31 | + Trace.addCategories(MapboxTraceCategory); |
| 32 | + Trace.enable(); |
| 33 | + } |
| 34 | + |
| 35 | + ngAfterViewInit() { |
| 36 | + console.log(' mapContainerRef: ', this.mapContainerRef.nativeElement); |
| 37 | + |
| 38 | + const contentView = this.mapContainerRef.nativeElement; |
| 39 | + |
| 40 | + const settings = { |
| 41 | + container: contentView, |
| 42 | + accessToken: MAPBOX_ACCESS_TOKEN, |
| 43 | + style: MapStyle.TRAFFIC_DAY, |
| 44 | + center: { |
| 45 | + lat: 50.681466, |
| 46 | + lng: 17.8687037 |
| 47 | + }, |
| 48 | + zoomLevel: 12, // 0 (most of the world) to 20, default 0 |
| 49 | + showUserLocation: false, // default false |
| 50 | + hideAttribution: true, // default false |
| 51 | + hideLogo: true, // default false |
| 52 | + hideCompass: false, // default false |
| 53 | + disableRotation: false, // default false |
| 54 | + disableScroll: false, // default false |
| 55 | + disableZoom: false, // default false |
| 56 | + disableTilt: false // default false |
| 57 | + }; |
| 58 | + |
| 59 | + const mapView = new MapboxView(); |
| 60 | + mapView.setConfig(settings); |
| 61 | + contentView.content = mapView; |
| 62 | + this.mapReady(mapView); |
| 63 | + } |
| 64 | + |
| 65 | + mapReady(mapView: MapboxView): void { |
| 66 | + mapView.on('mapReady', (args: any) => { |
| 67 | + this.mapboxView = args.map; |
| 68 | + this.mapbox = this.mapboxView.getMapboxApi(); |
| 69 | + this.cd.detectChanges(); |
| 70 | + console.log('mapboxView map is ready:', this.mapboxView); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + goBack(): void { |
| 75 | + this.router.back(); |
| 76 | + } |
| 77 | + |
| 78 | + ngOnDestroy(): void { |
| 79 | + if (this.mapbox) { |
| 80 | + this.mapbox.destroy(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments