Giter Site home page Giter Site logo

alexey3 / angular-esri-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tomwayson/angular-esri-loader

0.0 1.0 0.0 27 KB

An Angular service to help you load ArcGIS API for JavaScript Modules

License: Apache License 2.0

TypeScript 100.00%

angular-esri-loader's Introduction

angular-esri-loader

An Angular library to help you load ArcGIS API for JavaScript modules.

Exposes a service that wraps the functions from the esri-loader library in new functions that return promises.

To understand why this is needed, and the benefits of using esri-loader over other techniques, see the esri-loader README.

Install

For Angular 4 and above:

npm install angular-esri-loader

For Angular 2:

npm install angular2-esri-loader esri-loader

NOTE: for AngularJS use angular-esri-map.

Usage

Below is an example of using the loader service in a component to lazy load the ArcGIS API and create a map.

First you need to import the EsriLoaderModule into your application:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { EsriLoaderModule } from 'angular-esri-loader';

import { AppComponent } from './app.component';
import { EsriMapComponent } from './esri-map.component';

@NgModule({
  declarations: [
    AppComponent,
    EsriMapComponent
  ],
  imports: [
    BrowserModule,
    EsriLoaderModule,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Then you can use the EsriLoaderService in a component to load a map:

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { EsriLoaderService } from 'angular-esri-loader';

@Component({
  selector: 'app-esri-map',
  templateUrl: './esri-map.component.html',
  styleUrls: ['./esri-map.component.css']
})
export class EsriMapComponent implements OnInit {
  @ViewChild('map') mapEl: ElementRef;
  
  map: any;

  constructor(private esriLoader: EsriLoaderService) { }

  ngOnInit() {
    // only load the ArcGIS API for JavaScript when this component is loaded
    return this.esriLoader.load({
      // use a specific version of the API instead of the latest
      url: '//js.arcgis.com/3.18/'
    }).then(() => {
      // load the map class needed to create a new map
      this.esriLoader.loadModules(['esri/map']).then(([Map]) => {
        // create the map at the DOM element in this component
        this.map = new Map(this.mapEl.nativeElement, {
          center: [-118, 34.5],
          zoom: 8,
          basemap: 'dark-gray'
        });
      });
    });
  }
}

In an Angular CLI Application

To use this library in an Angular CLI application, the best place to start is to follow the instructions in this gist.

For an example of how to use this service to lazy load the ArcGIS API and resolve modules in a route, see esri-angular-cli-example's esri-map-resolve.service.ts.

In an angular2-webpack-starter Application

See this gist for instructions on how to use this library in an AngularClass/angular2-webpack-starter application.

In an Angular 2 Application

Example of using the loader service in a component to lazy load the ArcGIS API and create a map

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';

import { EsriLoaderService } from 'angular-esri-loader';

@Component({
  selector: 'app-esri-map',
  templateUrl: './esri-map.component.html',
  styleUrls: ['./esri-map.component.css']
})
export class EsriMapComponent implements OnInit {
  @ViewChild('map') mapEl: ElementRef;
  
  map: any;

  constructor(private esriLoader: EsriLoaderService) { }

  ngOnInit() {
    // only load the ArcGIS API for JavaScript when this component is loaded
    return this.esriLoader.load({
      // use a specific version of the API instead of the latest
      url: '//js.arcgis.com/3.18/'
    }).then(() => {
      // load the map class needed to create a new map
      this.esriLoader.loadModules(['esri/map']).then(([Map]) => {
        // create the map at the DOM element in this component
        this.map = new Map(this.mapEl.nativeElement, {
          center: [-118, 34.5],
          zoom: 8,
          basemap: 'dark-gray'
        });
      });
    });
  }
}

ArcGIS Types

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.

4.x Types

Follow these instructions to install the 4.x types.

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.

Then you can use the __esri namespace for the types as seen in this example.

3.x Types

Unfortunately the __esri namespace is not defined for 3.x types. You can use these instructions to install the 3.x types, but then you will still need to use import statements to get the types. This may cause build errors that may or may not result in actual runtime errors depending on your environment.

Examples

This service is in use in these applications:

Resources

Credit

Thanks to @kgs916 for helping me figure out how to publish a TypeScript library for Angular 2. I'll never do that again.

angular-esri-loader's People

Contributors

jwasilgeo avatar thekeithstewart avatar tomwayson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.