Skip to content

Instantly share code, notes, and snippets.

@andygup
Created September 25, 2019 17:43
Show Gist options
  • Save andygup/85c1376f3996819894438bb15af820ba to your computer and use it in GitHub Desktop.
Save andygup/85c1376f3996819894438bb15af820ba to your computer and use it in GitHub Desktop.
Webpack esri map component
/*
Copyright 2019 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { Component, OnInit, OnDestroy, ViewChild, ElementRef, Input, Output, EventEmitter } from '@angular/core';
import Map from "arcgis-js-api/Map";
import MapView from "arcgis-js-api/views/MapView";
import esri = __esri; // Esri TypeScript Types
@Component({
selector: 'app-esri-map',
templateUrl: './esri-map.component.html',
styleUrls: ['./esri-map.component.scss']
})
export class EsriMapComponent implements OnInit, OnDestroy {
@Output() mapLoadedEvent = new EventEmitter<boolean>();
// The <div> where we will place the map
@ViewChild('mapViewNode', {static: true}) private mapViewEl: ElementRef;
private _zoom = 10;
private _center: Array<number> = [0.1278, 51.5074];
private _basemap = 'streets';
private _loaded = false;
private _view: esri.MapView = null;
get mapLoaded(): boolean {
return this._loaded;
}
@Input()
set zoom(zoom: number) {
this._zoom = zoom;
}
get zoom(): number {
return this._zoom;
}
@Input()
set center(center: Array<number>) {
this._center = center;
}
get center(): Array<number> {
return this._center;
}
@Input()
set basemap(basemap: string) {
this._basemap = basemap;
}
get basemap(): string {
return this._basemap;
}
constructor() { }
async initializeMap() {
try {
// Configure the Map
const mapProperties: esri.MapProperties = {
basemap: this._basemap
};
const map: esri.Map = new Map(mapProperties);
// Initialize the MapView
const mapViewProperties: esri.MapViewProperties = {
container: this.mapViewEl.nativeElement,
center: this._center,
zoom: this._zoom,
map: map
};
this._view = new MapView(mapViewProperties);
await this._view.when();
return this._view;
} catch (error) {
console.log('ArcGIS: ', error);
}
}
ngOnInit() {
// Initialize MapView and return an instance of MapView
this.initializeMap().then((mapView) => {
// The map has been initialized
console.log('mapView ready: ', mapView.ready);
this._loaded = mapView.ready;
this.mapLoadedEvent.emit(true);
});
}
ngOnDestroy() {
this._view.container = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment