OpenLayers - chanandrew96/MyLearning GitHub Wiki
- Download the release from OpenLayers
- Unzip and copy the unzipped folder into your project
- Import the files using
<link>
and<script>
<link rel="stylesheet" href="./ol-package/ol.css">
<script src="./ol-package/dist/ol.js"></script>
Use the import
function to import all the components you need
Example:
import './style.css';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
Once you added ol.js
using the <script>
tag, you can use the component like ol.source.OSM()
or ol.layer.Tile
Sample code for using OpenLayers to show image:
const extent = [0, 0, 1024, 968];
const projection = new ol.proj.Projection({
code: 'xkcd-image',
units: 'pixels',
extent: extent,
});
// Create the Viewer
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
attributions: '<a href="https://dummyimage.com/600x400/000/fff">Dummy Image</a>',
url: 'https://dummyimage.com/600x400/000/fff',
projection: projection,
imageExtent: extent,
})
})
],
view: new ol.View({
projection: projection,
center: ol.extent.getCenter(extent),
zoom: 2,
maxZoom: 8,
})
});