├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.e2e.json ├── package-lock.json ├── package.json ├── src ├── app │ ├── app.component.css │ ├── app.component.html │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ └── map │ │ ├── control │ │ ├── control.component.ts │ │ └── mouse-position.component.ts │ │ ├── interaction │ │ └── interaction.component.ts │ │ ├── layer │ │ └── layer.component.ts │ │ ├── map.component.ts │ │ ├── map.module.ts │ │ ├── map.service.ts │ │ └── mapid.service.ts ├── assets │ └── .gitkeep ├── browserslist ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── karma.conf.js ├── main.ts ├── polyfills.ts ├── styles.css ├── test.ts ├── tsconfig.app.json ├── tsconfig.spec.json └── tslint.json ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # profiling files 12 | chrome-profiler-events.json 13 | speed-measure-plugin.json 14 | 15 | # IDEs and editors 16 | /.idea 17 | .project 18 | .classpath 19 | .c9/ 20 | *.launch 21 | .settings/ 22 | *.sublime-workspace 23 | 24 | # IDE - VSCode 25 | .vscode/* 26 | !.vscode/settings.json 27 | !.vscode/tasks.json 28 | !.vscode/launch.json 29 | !.vscode/extensions.json 30 | 31 | # misc 32 | /.sass-cache 33 | /connect.lock 34 | /coverage 35 | /libpeerconnection.log 36 | npm-debug.log 37 | yarn-error.log 38 | testem.log 39 | /typings 40 | 41 | # System Files 42 | .DS_Store 43 | Thumbs.db 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jean-Marc Viglino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #  ![](src/favicon.ico) ol-ext + Angular 2 | 3 | This project is an example to use [Openlayers](https://github.com/openlayers/openlayers) and [ol-ext](https://github.com/Viglino/ol-ext) with [Angular 7](https://angular.io/). 4 | 5 | > It is by no means a complete app but you should find mecanisms to handle an Openlayers map using Angular, with interactions and controls to customize for your own. 6 | 7 | ## Goal 8 | 9 | The goal of this example is to create a map as simple as that: 10 | ````html 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | ```` 23 | 24 | As controls can be set outside the map (using the target option) we also want to have this option too. 25 | ````html 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | ```` 36 | 37 | We also want to create more than one map: 38 | ````html 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | ```` 49 | 50 | ## Mecanisms 51 | 52 | ### MapService and MapidService 53 | 54 | The map is implemented as a service to let you access it in other components. 55 | 56 | #### Getting a map 57 | You just have to declare the service in your constructor to access the map: 58 | ````javascript 59 | constructor(private mapService: MapService) { } 60 | ```` 61 | Then to retrieve the map you want, use the `getMap` method: 62 | ````javascript 63 | // Default map (id=map) 64 | const map = mapService.getMap(); 65 | // another map (id=map1) 66 | const map1 = mapService.getMap('map1'); 67 | ```` 68 | The parameter is the id of the map you want to get. If you just have one map you can use the default value (`map`). 69 | 70 | #### Getting the current map 71 | 72 | The `MapidService` let you handle the current map's id. 73 | It is used by the map component to register a new map id (`mapServe.setId()`). 74 | It's not a root service and each map has its own one, thus each component defined inside a map component can access the current map id using the `getId()` method of the service. 75 | 76 | This id is registered by the root `MapComponent` (using the `setId()`method). 77 | 78 | 79 | #### Customizing the map 80 | 81 | Feel free to modify the `createMap()` method of the `MapService` to handle the default map. 82 | The `MapComponent` let you define the map itself. Use the `ngOnInit()` method to customize the map (set zoom, etc.). 83 | 84 | ### Creating new map components (controls, layers, interactions...) 85 | 86 | This example comes with a set of components for each Openlayers map features: controls, interactions, layer... 87 | Just copy the `.ts` file to create a new one to use in your app. 88 | 89 | You first have to declare the services in your constructor: 90 | ````javascript 91 | constructor( 92 | private mapService: MapService, 93 | @Host() 94 | private mapidService: MapidService 95 | ) { } 96 | ```` 97 | Then in ngOnInit, get the current map like this: 98 | ````javascript 99 | ngOnInit() { 100 | // Get the current map 101 | const map: OlMap = this.mapService.getMap(this.mapidService); 102 | // Add component to the map 103 | } 104 | ```` 105 | 106 | To let the control be set inside or outside the map you also need to get the target ElementRef. In this case the MapidService is optional (as it comes inside a MapComponent it is not defined when set outside a map). 107 | Look at the [ControlComponent](src/app/map/control/control.component.ts) for more informations. 108 | 109 | #### Example 110 | 111 | The example defines: 112 | * 2 maps 113 | * a set of layers define using a component propertie 114 | * an interaction to synchonize the maps together 115 | * a control inside the map (Bookmark contol) 116 | * a control outside the map (MousePosition). 117 | 118 | 119 | ## Build 120 | 121 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.1.4. 122 | 123 | ### Install 124 | 125 | Run `npm install` to install node modules and start developping. 126 | 127 | ### Development server 128 | 129 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 130 | 131 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 132 | 133 | ### Further help 134 | 135 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 136 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "ol-ext-angular": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": {}, 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/ol-ext-angular", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "src/tsconfig.app.json", 21 | "assets": [ 22 | "src/favicon.ico", 23 | "src/assets" 24 | ], 25 | "styles": [ 26 | "src/styles.css" 27 | ], 28 | "scripts": [] 29 | }, 30 | "configurations": { 31 | "production": { 32 | "fileReplacements": [ 33 | { 34 | "replace": "src/environments/environment.ts", 35 | "with": "src/environments/environment.prod.ts" 36 | } 37 | ], 38 | "optimization": true, 39 | "outputHashing": "all", 40 | "sourceMap": false, 41 | "extractCss": true, 42 | "namedChunks": false, 43 | "aot": true, 44 | "extractLicenses": true, 45 | "vendorChunk": false, 46 | "buildOptimizer": false, 47 | "budgets": [ 48 | { 49 | "type": "initial", 50 | "maximumWarning": "2mb", 51 | "maximumError": "5mb" 52 | } 53 | ] 54 | } 55 | } 56 | }, 57 | "serve": { 58 | "builder": "@angular-devkit/build-angular:dev-server", 59 | "options": { 60 | "browserTarget": "ol-ext-angular:build" 61 | }, 62 | "configurations": { 63 | "production": { 64 | "browserTarget": "ol-ext-angular:build:production" 65 | } 66 | } 67 | }, 68 | "extract-i18n": { 69 | "builder": "@angular-devkit/build-angular:extract-i18n", 70 | "options": { 71 | "browserTarget": "ol-ext-angular:build" 72 | } 73 | }, 74 | "test": { 75 | "builder": "@angular-devkit/build-angular:karma", 76 | "options": { 77 | "main": "src/test.ts", 78 | "polyfills": "src/polyfills.ts", 79 | "tsConfig": "src/tsconfig.spec.json", 80 | "karmaConfig": "src/karma.conf.js", 81 | "styles": [ 82 | "src/styles.css" 83 | ], 84 | "scripts": [], 85 | "assets": [ 86 | "src/favicon.ico", 87 | "src/assets" 88 | ] 89 | } 90 | }, 91 | "lint": { 92 | "builder": "@angular-devkit/build-angular:tslint", 93 | "options": { 94 | "tsConfig": [ 95 | "src/tsconfig.app.json", 96 | "src/tsconfig.spec.json" 97 | ], 98 | "exclude": [ 99 | "**/node_modules/**" 100 | ] 101 | } 102 | } 103 | } 104 | }, 105 | "ol-ext-angular-e2e": { 106 | "root": "e2e/", 107 | "projectType": "application", 108 | "prefix": "", 109 | "architect": { 110 | "e2e": { 111 | "builder": "@angular-devkit/build-angular:protractor", 112 | "options": { 113 | "protractorConfig": "e2e/protractor.conf.js", 114 | "devServerTarget": "ol-ext-angular:serve" 115 | }, 116 | "configurations": { 117 | "production": { 118 | "devServerTarget": "ol-ext-angular:serve:production" 119 | } 120 | } 121 | }, 122 | "lint": { 123 | "builder": "@angular-devkit/build-angular:tslint", 124 | "options": { 125 | "tsConfig": "e2e/tsconfig.e2e.json", 126 | "exclude": [ 127 | "**/node_modules/**" 128 | ] 129 | } 130 | } 131 | } 132 | } 133 | }, 134 | "defaultProject": "ol-ext-angular" 135 | } -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getTitleText()).toEqual('Welcome to ol-ext-angular!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ol-ext-angular", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~7.1.0", 15 | "@angular/common": "~7.1.0", 16 | "@angular/compiler": "~7.1.0", 17 | "@angular/core": "~7.1.0", 18 | "@angular/forms": "~7.1.0", 19 | "@angular/platform-browser": "~7.1.0", 20 | "@angular/platform-browser-dynamic": "~7.1.0", 21 | "@angular/router": "~7.1.0", 22 | "core-js": "^2.5.4", 23 | "ol-ext": "^3.1.0", 24 | "rxjs": "~6.3.3", 25 | "tslib": "^1.9.0", 26 | "zone.js": "~0.8.26" 27 | }, 28 | "devDependencies": { 29 | "@angular-devkit/build-angular": "~0.11.0", 30 | "@angular/cli": "~7.1.4", 31 | "@angular/compiler-cli": "~7.1.0", 32 | "@angular/language-service": "~7.1.0", 33 | "@types/jasmine": "~2.8.8", 34 | "@types/jasminewd2": "~2.0.3", 35 | "@types/node": "~8.9.4", 36 | "codelyzer": "~4.5.0", 37 | "jasmine-core": "~2.99.1", 38 | "jasmine-spec-reporter": "~4.2.1", 39 | "karma": "~3.1.1", 40 | "karma-chrome-launcher": "~2.2.0", 41 | "karma-coverage-istanbul-reporter": "~2.0.1", 42 | "karma-jasmine": "~1.1.2", 43 | "karma-jasmine-html-reporter": "^0.2.2", 44 | "protractor": "~5.4.0", 45 | "ts-node": "~7.0.0", 46 | "tslint": "~5.11.0", 47 | "typescript": "~3.1.6" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- 1 | app-map { 2 | position: fixed; 3 | top: 0; 4 | left: 0; 5 | bottom: 2em; 6 | width: 50%; 7 | } 8 | #map1 { 9 | right: 0; 10 | left: auto; 11 | } 12 | 13 | .ctrl { 14 | position: fixed; 15 | right: 0; 16 | background: #fff; 17 | } 18 | .ctrl >>> .ol-bookmark div { 19 | display: block!important; 20 | } 21 | 22 | app-mouse-position { 23 | position: absolute; 24 | bottom:0; 25 | left: 0; 26 | right: 0; 27 | font-size: 1.5em; 28 | text-align: center; 29 | font-family: sans-serif; 30 | } 31 | app-mouse-position >>> div { 32 | bottom: 0; 33 | top: auto; 34 | left: 0; 35 | } -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(async(() => { 6 | TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ], 10 | }).compileComponents(); 11 | })); 12 | 13 | it('should create the app', () => { 14 | const fixture = TestBed.createComponent(AppComponent); 15 | const app = fixture.debugElement.componentInstance; 16 | expect(app).toBeTruthy(); 17 | }); 18 | 19 | it(`should have as title 'ol-ext-angular'`, () => { 20 | const fixture = TestBed.createComponent(AppComponent); 21 | const app = fixture.debugElement.componentInstance; 22 | expect(app.title).toEqual('ol-ext-angular'); 23 | }); 24 | 25 | it('should render title in a h1 tag', () => { 26 | const fixture = TestBed.createComponent(AppComponent); 27 | fixture.detectChanges(); 28 | const compiled = fixture.debugElement.nativeElement; 29 | expect(compiled.querySelector('h1').textContent).toContain('Welcome to ol-ext-angular!'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.css'] 7 | }) 8 | export class AppComponent { 9 | title = 'ol-ext-angular'; 10 | } 11 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | 4 | import { AppComponent } from './app.component'; 5 | 6 | // Import Map module 7 | import { MapModule } from './map/map.module'; 8 | 9 | @NgModule({ 10 | declarations: [ 11 | AppComponent 12 | ], 13 | imports: [ 14 | BrowserModule, 15 | MapModule 16 | ], 17 | providers: [], 18 | bootstrap: [AppComponent] 19 | }) 20 | export class AppModule { } 21 | -------------------------------------------------------------------------------- /src/app/map/control/control.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, ElementRef, OnInit, Host, Optional } from '@angular/core'; 2 | 3 | import { MapService } from '../map.service'; 4 | import { MapidService } from '../mapid.service'; 5 | import OlMap from 'ol/Map'; 6 | import BookmarkCtrl from 'ol-ext/control/GeoBookmark'; 7 | 8 | /** 9 | * Add a control to the map 10 | * The control can be set inside the map (using parent id) or outside (using a mapId attribute) 11 | * @example 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | */ 20 | @Component({ 21 | selector: 'app-control', 22 | template: '' 23 | }) 24 | 25 | export class ControlComponent implements OnInit { 26 | 27 | /** Map id 28 | */ 29 | @Input() mapId: string; 30 | 31 | /** Define the service 32 | */ 33 | constructor( 34 | private mapService: MapService, 35 | @Host() 36 | @Optional() 37 | private mapidService: MapidService, 38 | private elementRef: ElementRef 39 | ) { } 40 | 41 | /** Add the control to the map 42 | */ 43 | ngOnInit() { 44 | // Get the current map or get map by id 45 | const map: OlMap = this.mapService.getMap(this.mapidService || this.mapId); 46 | // Get the target if outside the map 47 | const target = this.elementRef.nativeElement.parentElement ? this.elementRef.nativeElement : null; 48 | // Create the control 49 | const mark = new BookmarkCtrl({ target: target }); 50 | map.addControl(mark); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/app/map/control/mouse-position.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, ElementRef, OnInit, Host, Optional } from '@angular/core'; 2 | 3 | import { MapService } from '../map.service'; 4 | import { MapidService } from '../mapid.service'; 5 | import OlMap from 'ol/Map'; 6 | import MousePoisition from 'ol/control/MousePosition'; 7 | import {createStringXY} from 'ol/coordinate'; 8 | 9 | /** 10 | * Add a Mouse position control to the map 11 | * The control can be set inside the map (using parent id) or outside (using a mapId attribute) 12 | */ 13 | @Component({ 14 | selector: 'app-mouse-position', 15 | template: '' 16 | }) 17 | 18 | export class MousePositionComponent implements OnInit { 19 | 20 | /** Map id 21 | */ 22 | @Input() mapId: string; 23 | 24 | /** 25 | */ 26 | constructor( 27 | private mapService: MapService, 28 | @Host() 29 | @Optional() 30 | private mapidService: MapidService, 31 | private elementRef: ElementRef 32 | ) { } 33 | 34 | ngOnInit() { 35 | // Get the current map or get map by id 36 | const map: OlMap = this.mapService.getMap(this.mapidService || this.mapId); 37 | // Get the target if outside the map 38 | const target = this.elementRef.nativeElement.parentElement ? this.elementRef.nativeElement : null; 39 | // Create the control 40 | const ctrl = new MousePoisition({ 41 | coordinateFormat: createStringXY(4), 42 | projection: 'EPSG:4326', 43 | undefinedHTML: ' ', 44 | target: target 45 | }); 46 | map.addControl(ctrl); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/app/map/interaction/interaction.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, OnInit, Host } from '@angular/core'; 2 | 3 | import { MapService } from '../map.service'; 4 | import { MapidService } from '../mapid.service'; 5 | import OlMap from 'ol/Map'; 6 | import Synchronize from 'ol-ext/interaction/Synchronize'; 7 | 8 | /** 9 | * Add interactions to a map 10 | * @example 11 | 12 | 13 | 14 | */ 15 | @Component({ 16 | selector: 'app-interaction', 17 | template: '' 18 | }) 19 | 20 | export class InteractionComponent implements OnInit { 21 | 22 | /** Define the service 23 | */ 24 | constructor( 25 | private mapService: MapService, 26 | @Host() 27 | private mapidService: MapidService 28 | ) { } 29 | 30 | /** Add new interaction to the map 31 | */ 32 | ngOnInit() { 33 | // Get the current map 34 | const map: OlMap = this.mapService.getMap(this.mapidService); 35 | // Get the second map to synchronize 36 | const mapId = this.mapidService.getId(); 37 | const map2 = (mapId === 'map1' ? 'map' : 'map1'); 38 | // Add interaction 39 | const sync = new Synchronize({ maps: [ this.mapService.getMap(map2) ] }); 40 | map.addInteraction(sync); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/app/map/layer/layer.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, OnInit, Host } from '@angular/core'; 2 | 3 | import { MapService } from '../map.service'; 4 | import { MapidService } from '../mapid.service'; 5 | import OlMap from 'ol/Map'; 6 | import OSM from 'ol/source/OSM'; 7 | import Stamen from 'ol/source/Stamen'; 8 | import OlTileLayer from 'ol/layer/Tile'; 9 | 10 | /** 11 | * Add layers to a map 12 | * @example 13 | 14 | 15 | 16 | */ 17 | @Component({ 18 | selector: 'app-layer', 19 | template: '' 20 | }) 21 | 22 | export class LayerComponent implements OnInit { 23 | /** Layer */ 24 | @Input() layer; 25 | /** Layer opacity */ 26 | @Input() name; 27 | /** Layer opacity */ 28 | @Input() opacity = 1; 29 | /** Layer visibility */ 30 | @Input() visibility = true; 31 | 32 | /** Define the service 33 | */ 34 | constructor( 35 | private mapService: MapService, 36 | @Host() 37 | private mapidService: MapidService 38 | ) { } 39 | 40 | /** Add layer to the map 41 | */ 42 | ngOnInit() { 43 | // Get the current map 44 | const map: OlMap = this.mapService.getMap(this.mapidService); 45 | // Add the layer 46 | let layer; 47 | switch (this.layer) { 48 | case 'watercolor': { 49 | layer = new OlTileLayer({ 50 | source: new Stamen({ layer: 'watercolor' }) 51 | }); 52 | break; 53 | } 54 | case 'labels': { 55 | layer = new OlTileLayer({ 56 | source: new Stamen({ layer: 'toner-labels' }) 57 | }); 58 | break; 59 | } 60 | case 'OSM': 61 | default: { 62 | layer = new OlTileLayer({ 63 | source: new OSM() 64 | }); 65 | } 66 | } 67 | layer.set('name', this.name || this.layer); 68 | layer.setOpacity(this.opacity); 69 | layer.setVisible(this.visibility); 70 | map.addLayer(layer); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/app/map/map.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, Input, ElementRef } from '@angular/core'; 2 | import { ViewEncapsulation } from '@angular/core'; 3 | 4 | import OlMap from 'ol/Map'; 5 | import { fromLonLat } from 'ol/proj'; 6 | import { MapService } from './map.service'; 7 | import { MapidService } from './mapid.service'; 8 | 9 | /** 10 | * Map Component: load and display a map 11 | * @example 12 | * 13 | */ 14 | @Component({ 15 | selector: 'app-map', 16 | template: '', 17 | // Include ol style as global 18 | encapsulation: ViewEncapsulation.None, 19 | styleUrls: [ 20 | '../../../node_modules/ol/ol.css', 21 | '../../../node_modules/ol-ext/dist/ol-ext.css' 22 | ], 23 | providers: [MapidService] 24 | }) 25 | 26 | export class MapComponent implements OnInit { 27 | 28 | /** Map id 29 | */ 30 | @Input() id: string; 31 | 32 | /** Longitude of the map 33 | */ 34 | @Input() lon: string; 35 | 36 | /** Latitude of the map 37 | */ 38 | @Input() lat: string; 39 | 40 | /** Zoom of the map 41 | */ 42 | @Input() zoom: string; 43 | 44 | /** 45 | * [ol.Map](http://openlayers.org/en/latest/apidoc/ol.Map.html) Openlayer map object 46 | */ 47 | map: OlMap; 48 | 49 | constructor( 50 | private mapService: MapService, 51 | private mapidService: MapidService, 52 | private elementRef: ElementRef 53 | ) {} 54 | 55 | /** 56 | * Create map on Init 57 | */ 58 | ngOnInit() { 59 | // Register a new id in the Mapid Service 60 | this.mapidService.setId(this.id); 61 | // Create a new map 62 | this.map = this.mapService.getMap(this.id); 63 | // If id is not defined place the map in the component element itself 64 | if (!this.id) { 65 | this.id = 'map'; 66 | this.map.setTarget(this.elementRef.nativeElement); 67 | } 68 | // Center on attribute 69 | this.map.getView().setCenter(fromLonLat([parseFloat(this.lon) || 0, parseFloat(this.lat) || 0])); 70 | this.map.getView().setZoom(this.zoom); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/app/map/map.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | 3 | import { MapService } from './map.service'; 4 | 5 | import { MapComponent } from './map.component'; 6 | import { LayerComponent } from './layer/layer.component'; 7 | import { ControlComponent } from './control/control.component'; 8 | import { MousePositionComponent } from './control/mouse-position.component'; 9 | import { InteractionComponent } from './interaction/interaction.component'; 10 | 11 | @NgModule({ 12 | declarations: [ 13 | MapComponent, 14 | LayerComponent, 15 | ControlComponent, 16 | MousePositionComponent, 17 | InteractionComponent 18 | ], 19 | imports: [], 20 | providers: [ 21 | MapService 22 | ], 23 | exports: [ 24 | MapComponent, 25 | LayerComponent, 26 | ControlComponent, 27 | MousePositionComponent, 28 | InteractionComponent 29 | ] 30 | }) 31 | export class MapModule { } 32 | -------------------------------------------------------------------------------- /src/app/map/map.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | 3 | import OlMap from 'ol/Map'; 4 | import OlView from 'ol/View'; 5 | 6 | /** 7 | * Openlayers map service to acces maps by id 8 | * Inject the service in the class that have to use it and access the map with the getMap method. 9 | * @example 10 | * 11 | import { MapService } from '../map.service'; 12 | import OlMap from 'ol/Map'; 13 | 14 | constructor( 15 | private mapService: MapService, 16 | ) { } 17 | ngOnInit() { 18 | // Get the current map 19 | const map: OlMap = this.mapService.getMap('map'); 20 | } 21 | */ 22 | @Injectable({ 23 | providedIn: 'root' 24 | }) 25 | export class MapService { 26 | 27 | /** 28 | * List of Openlayer map objects [ol.Map](https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html) 29 | */ 30 | private map = {}; 31 | 32 | constructor() { } 33 | 34 | /** 35 | * Create a map 36 | * @param id map id 37 | * @returns [ol.Map](https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html) the map 38 | */ 39 | private createMap(id): OlMap { 40 | const map = new OlMap({ 41 | target: id, 42 | view: new OlView({ 43 | center: [0, 0], 44 | zoom: 1, 45 | projection: 'EPSG:3857' 46 | }) 47 | }); 48 | return map; 49 | } 50 | 51 | /** 52 | * Get a map. If it doesn't exist it will be created. 53 | * @param id id of the map or an objet with a getId method (from mapid service), default 'map' 54 | */ 55 | getMap(id): OlMap { 56 | id = ((id && id.getId) ? id.getId() : id ) || 'map'; 57 | // Create map if not exist 58 | if (!this.map[id]) { 59 | this.map[id] = this.createMap(id); 60 | } 61 | // return the map 62 | return this.map[id]; 63 | } 64 | 65 | /** Get all maps 66 | * NB: to access the complete list of maps you should use the ngAfterViewInit() method to have all maps instanced. 67 | * @return the list of maps 68 | */ 69 | getMaps() { 70 | return this.map; 71 | } 72 | 73 | /** Get all maps 74 | * NB: to access the complete list of maps you should use the ngAfterViewInit() method to have all maps instanced. 75 | * @return array of maps 76 | */ 77 | getArrayMaps() { 78 | return Object.values(this.map); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/app/map/mapid.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | 3 | /** 4 | * Service to get the current id (declared by the host). 5 | * Inject the service in the class that have to use it 6 | * and access the current id with the getId function. 7 | * You can also use the setId to enable the subcomponent to access the id in the host. 8 | * @example 9 | constructor( 10 | @Host() 11 | @Optional() // use optional to mal the service optional 12 | private mapidService: MapidService 13 | ) 14 | ngOnInit() { 15 | // Get the current map id 16 | const mapId = this.mapidService.getId(); 17 | } 18 | */ 19 | @Injectable() 20 | export class MapidService { 21 | 22 | /** 23 | * The current id 24 | */ 25 | private id: string; 26 | 27 | constructor() { } 28 | 29 | /** 30 | * Get Id of the map, default 'map' 31 | */ 32 | getId(): string { 33 | return this.id; 34 | } 35 | 36 | /** 37 | * Set Id 38 | */ 39 | setId(id: string= null) { 40 | this.id = id; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Viglino/ol-ext-angular/3d0c5950e22e459fbdae94ff2afea086a8dceaab/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Viglino/ol-ext-angular/3d0c5950e22e459fbdae94ff2afea086a8dceaab/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | OlExtAngular 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false 30 | }); 31 | }; -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10, IE11, and Chrome <55 requires all of the following polyfills. 22 | * This also includes Android Emulators with older versions of Chrome and Google Search/Googlebot 23 | */ 24 | 25 | // import 'core-js/es6/symbol'; 26 | // import 'core-js/es6/object'; 27 | // import 'core-js/es6/function'; 28 | // import 'core-js/es6/parse-int'; 29 | // import 'core-js/es6/parse-float'; 30 | // import 'core-js/es6/number'; 31 | // import 'core-js/es6/math'; 32 | // import 'core-js/es6/string'; 33 | // import 'core-js/es6/date'; 34 | // import 'core-js/es6/array'; 35 | // import 'core-js/es6/regexp'; 36 | // import 'core-js/es6/map'; 37 | // import 'core-js/es6/weak-map'; 38 | // import 'core-js/es6/set'; 39 | 40 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 41 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 42 | 43 | /** IE10 and IE11 requires the following for the Reflect API. */ 44 | // import 'core-js/es6/reflect'; 45 | 46 | /** 47 | * Web Animations `@angular/platform-browser/animations` 48 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 49 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 50 | */ 51 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 52 | 53 | /** 54 | * By default, zone.js will patch all possible macroTask and DomEvents 55 | * user can disable parts of macroTask/DomEvents patch by setting following flags 56 | * because those flags need to be set before `zone.js` being loaded, and webpack 57 | * will put import in the top of bundle, so user need to create a separate file 58 | * in this directory (for example: zone-flags.ts), and put the following flags 59 | * into that file, and then add the following code before importing zone.js. 60 | * import './zone-flags.ts'; 61 | * 62 | * The flags allowed in zone-flags.ts are listed here. 63 | * 64 | * The following flags will work for all browsers. 65 | * 66 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 67 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 68 | * (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 69 | * 70 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 71 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 72 | * 73 | * (window as any).__Zone_enable_cross_context_check = true; 74 | * 75 | */ 76 | 77 | /*************************************************************************************************** 78 | * Zone JS is required by default for Angular itself. 79 | */ 80 | import 'zone.js/dist/zone'; // Included with Angular CLI. 81 | 82 | 83 | /*************************************************************************************************** 84 | * APPLICATION IMPORTS 85 | */ 86 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "target": "es5", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-redundant-jsdoc": true, 69 | "no-shadowed-variable": true, 70 | "no-string-literal": false, 71 | "no-string-throw": true, 72 | "no-switch-case-fall-through": true, 73 | "no-trailing-whitespace": true, 74 | "no-unnecessary-initializer": true, 75 | "no-unused-expression": true, 76 | "no-use-before-declare": true, 77 | "no-var-keyword": true, 78 | "object-literal-sort-keys": false, 79 | "one-line": [ 80 | true, 81 | "check-open-brace", 82 | "check-catch", 83 | "check-else", 84 | "check-whitespace" 85 | ], 86 | "prefer-const": true, 87 | "quotemark": [ 88 | true, 89 | "single" 90 | ], 91 | "radix": true, 92 | "semicolon": [ 93 | true, 94 | "always" 95 | ], 96 | "triple-equals": [ 97 | true, 98 | "allow-null-check" 99 | ], 100 | "typedef-whitespace": [ 101 | true, 102 | { 103 | "call-signature": "nospace", 104 | "index-signature": "nospace", 105 | "parameter": "nospace", 106 | "property-declaration": "nospace", 107 | "variable-declaration": "nospace" 108 | } 109 | ], 110 | "unified-signatures": true, 111 | "variable-name": false, 112 | "whitespace": [ 113 | true, 114 | "check-branch", 115 | "check-decl", 116 | "check-operator", 117 | "check-separator", 118 | "check-type" 119 | ], 120 | "no-output-on-prefix": true, 121 | "use-input-property-decorator": true, 122 | "use-output-property-decorator": true, 123 | "use-host-property-decorator": true, 124 | "no-input-rename": true, 125 | "no-output-rename": true, 126 | "use-life-cycle-interface": true, 127 | "use-pipe-transform-interface": true, 128 | "component-class-suffix": true, 129 | "directive-class-suffix": true 130 | } 131 | } 132 | --------------------------------------------------------------------------------