Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install OpenLayers using NPM

There seem to be two OpenLayers packages available over NPM:

Option 1

npm install ol

which then can be used:

import OlMap from 'ol/map';
import OlView from 'ol/view';
import OlTile from 'ol/layer/tile';
import OlLayerVector from 'ol/layer/vector';
import OlSourceVector from 'ol/source/vector';

Option 2

npm install openlayers
import * as ol from 'openlayers';

Why two packages? What is the correct way, if any?

The second one looks more elegant to me, but OL's documentation actually mentions the first one: https://www.npmjs.com/package/openlayers

like image 651
Lucas Avatar asked Jan 15 '18 22:01

Lucas


People also ask

How do I import to OL?

Typical OpenLayers example import 'ol/ol. css'; import {Map, View} from 'ol'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 0 }) });


1 Answers

The NPM page explains the difference. Do not start a project with openlayers, it uses closure, which you most likely will not. ol is packaged as'state of art' ES2015 modules. It enables your compiler (eg webpack) to only package things you actually use.

For use with webpack, Rollup, Browserify, or other module bundlers, install the ol package:

npm install ol

For use with Closure Library (rare), install the openlayers package and read the tutorial.

npm install openlayers

like image 106
dube Avatar answered Sep 24 '22 15:09

dube