Skip to content

Instantly share code, notes, and snippets.

@andygup
Last active July 25, 2019 23:03
Show Gist options
  • Save andygup/b4736f8a7cc97a007a196dcb42af74cf to your computer and use it in GitHub Desktop.
Save andygup/b4736f8a7cc97a007a196dcb42af74cf to your computer and use it in GitHub Desktop.
ArcGIS JavaScript map component using webpack imports with Angular 8
/*
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, 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 {
@Output() mapLoadedEvent = new EventEmitter<boolean>();
// The <div> where we will place the map
@ViewChild('mapViewNode', {static: true}) private mapViewEl: ElementRef;
/**
* _zoom sets map zoom
* _center sets map center
* _basemap sets type of map
* _loaded provides map loaded status
*/
private _zoom = 10;
private _center: Array<number> = [0.1278, 51.5074];
private _basemap = 'streets';
private _loaded = false;
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
};
return new MapView(mapViewProperties);
} catch (error) {
console.log('EsriLoader: ', error);
}
}
// Finalize a few things once the MapView has been loaded
houseKeeping(mapView) {
mapView.when(() => {
console.log('mapView ready: ', mapView.ready);
this._loaded = mapView.ready;
this.mapLoadedEvent.emit(true);
});
}
ngOnInit() {
// Initialize MapView and return an instance of MapView
this.initializeMap().then((mapView) => {
this.houseKeeping(mapView);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment