|
1 | 1 | # angular2-esri-loader
|
2 |
| -An Angular 2 service to help you load [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/) Modules |
| 2 | +An [Angular](https://angular.io/) library to help you load [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/) modules. |
| 3 | + |
| 4 | +Exposes a service that wraps the functions from the [esri-loader](https://github.com/tomwayson/esri-loader) library in new functions that return promises. |
| 5 | + |
| 6 | +To understand why this is needed, and the benefits of using esri-loader over other techniques, see the [esri-loader README](https://github.com/tomwayson/esri-loader#why-is-this-needed). |
3 | 7 |
|
4 | 8 | ## Install
|
5 | 9 | ```bash
|
6 | 10 | npm install angular2-esri-loader esri-loader
|
7 | 11 | ```
|
8 | 12 |
|
9 | 13 | ## Usage
|
10 |
| -Example of using the loader service in a resolver to lazy load the ArcGIS API and |
| 14 | +Example of using the loader service in a component to lazy load the ArcGIS API and create a map |
11 | 15 |
|
12 | 16 | ```ts
|
13 |
| -import { Injectable } from '@angular/core'; |
14 |
| -import { Resolve } from '@angular/router'; |
| 17 | +import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; |
15 | 18 |
|
16 | 19 | import { EsriLoaderService } from 'angular2-esri-loader';
|
17 | 20 |
|
18 |
| -@Injectable() |
19 |
| -export class EsriMapResolveService implements Resolve<any> { |
| 21 | +@Component({ |
| 22 | + selector: 'app-esri-map', |
| 23 | + templateUrl: './esri-map.component.html', |
| 24 | + styleUrls: ['./esri-map.component.css'] |
| 25 | +}) |
| 26 | +export class EsriMapComponent implements OnInit { |
| 27 | + @ViewChild('map') mapEl: ElementRef; |
| 28 | + |
| 29 | + map: any; |
20 | 30 |
|
21 | 31 | constructor(private esriLoader: EsriLoaderService) { }
|
22 | 32 |
|
23 |
| - resolve() { |
24 |
| - // only load the ArcGIS API for JavaScript when we get to this route |
| 33 | + ngOnInit() { |
| 34 | + // only load the ArcGIS API for JavaScript when this component is loaded |
25 | 35 | return this.esriLoader.load({
|
26 | 36 | // use a specific version of the API instead of the latest
|
27 | 37 | url: '//js.arcgis.com/3.18/'
|
28 | 38 | }).then(() => {
|
29 | 39 | // load the map class needed to create a new map
|
30 |
| - // and make it available in the route's data |
31 |
| - return this.esriLoader.loadModules(['esri/map']); |
| 40 | + this.esriLoader.loadModules(['esri/map']).then(([Map]) => { |
| 41 | + // create the map at the DOM element in this component |
| 42 | + this.map = new Map(this.mapEl.nativeElement, { |
| 43 | + center: [-118, 34.5], |
| 44 | + zoom: 8, |
| 45 | + basemap: 'dark-gray' |
| 46 | + }); |
| 47 | + }); |
32 | 48 | });
|
33 | 49 | }
|
34 | 50 | }
|
35 | 51 | ```
|
36 |
| - |
| 52 | + |
| 53 | +### In an Angular CLI Application |
| 54 | + |
| 55 | +To use this library in an [Angular CLI](https://github.com/angular/angular-cli) application, the best place to start is to follow the instructions in [this gist](https://gist.github.com/tomwayson/e6260adfd56c2529313936528b8adacd). |
| 56 | + |
| 57 | +For an example of how to use this service to lazy load the ArcGIS API and resolve modules in a route, see |
| 58 | +[esri-angular-cli-example's esri-map-resolve.service.ts](https://github.com/tomwayson/esri-angular-cli-example/blob/ab4540912904cf78ccfd904fb3bfa4c69b4aa1da/src/app/esri-map/esri-map-resolve.service.ts). |
| 59 | + |
| 60 | +### In an angular2-webpack-starter Application |
| 61 | + |
| 62 | +See [this gist](https://gist.github.com/jwasilgeo/00855ee002aca822e33abd8a7a031f56) for instructions on how to use this library in an [AngularClass/angular2-webpack-starter](https://github.com/AngularClass/angular2-webpack-starter) application. |
| 63 | + |
| 64 | +## ArcGIS Types |
| 65 | +This service doesn't make any assumptions about which version of the ArcGIS API you are using, so you will have to manually install the appropriate types. |
| 66 | + |
| 67 | +### 4.x Types |
| 68 | +Follow [these instructions](https://github.com/Esri/jsapi-resources/tree/master/4.x/typescript) to install the 4.x types. |
| 69 | + |
| 70 | +For Angular CLI applications, you will also need to add "arcgis-js-api" to `compilerOptions.types` in src/tsconfig.app.json and src/tsconfig.spec.json [as shown here](https://gist.github.com/tomwayson/e6260adfd56c2529313936528b8adacd#adding-the-arcgis-api-for-javascript-types). |
| 71 | + |
| 72 | +Then you can use the `__esri` namespace for the types as seen in [this example](https://github.com/kgs916/angular2-esri4-components/blob/68861b286fd3a4814c495c2bd723e336e917ced2/src/lib/esri4-map/esri4-map.component.ts#L20-L26). |
| 73 | + |
| 74 | +### 3.x Types |
| 75 | +Unfortunately the `__esri` namespace is not defined for 3.x types. You can use [these instructions](https://github.com/Esri/jsapi-resources/tree/master/3.x/typescript) to install the 3.x types, but then [you will still need to use `import` statements to get the types](https://github.com/Esri/jsapi-resources/issues/60). This may cause build errors that may or may not result in actual runtime errors depending on your environment. |
| 76 | + |
| 77 | +## Examples |
| 78 | +This service is in use in these applications: |
| 79 | +- [esri-angular-cli-example](https://github.com/tomwayson/esri-angular-cli-example) |
| 80 | + |
| 81 | +## Resources |
| 82 | +* [Using the ArcGIS API for JavaScript in Applications built with webpack](http://tomwayson.com/2016/11/27/using-the-arcgis-api-for-javascript-in-applications-built-with-webpack/) |
| 83 | +* [esri-loader](https://github.com/tomwayson/esri-loader) |
| 84 | + |
| 85 | +## Credit |
| 86 | +Thanks to [@kgs916](https://github.com/kgs916) for helping me figure out how to publish a TypeScript library for Angular 2. I'll never do that again. |
0 commit comments