### Create and Start a New OpenLayers Project Source: https://openlayers.org/doc/quickstart.html Use `npm create ol-app` to scaffold a new project, `cd` into the directory, and `npm start` to launch the development server. ```bash npm create ol-app my-app cd my-app npm start ``` -------------------------------- ### Install Proj4js Library Source: https://openlayers.org/doc/tutorials/raster-reprojection.html Install the Proj4js library using npm, which is required for handling custom projections not supported by default in OpenLayers. ```bash npm install proj4 ``` -------------------------------- ### Complete Map Initialization Example Source: https://openlayers.org/doc/tutorials/concepts.html Combines all essential components (Map, View, Source, Layer) into a single script to render a functional map with OpenStreetMap tiles. ```javascript import Map from 'ol/Map.js'; import View from 'ol/View.js'; import OSM from 'ol/source/OSM.js'; import TileLayer from 'ol/layer/Tile.js'; new Map({ layers: [ new TileLayer({source: new OSM()}), ], view: new View({ center: [0, 0], zoom: 2, }), target: 'map', }); ``` -------------------------------- ### Initialize Map with Corrected Coordinates using fromLonLat Source: https://openlayers.org/doc/faq.html This example demonstrates the correct way to set the map center by transforming longitude and latitude coordinates to the Web Mercator projection using `fromLonLat()` before passing them to the map view. ```javascript import Map from 'ol/Map.js'; import View from 'ol/View.js'; import TileLayer from 'ol/layer/Tile.js'; import OSM from 'ol/source/OSM.js'; import {fromLonLat} from 'ol/proj.js'; const washingtonLonLat = [-77.036667, 38.895]; const washingtonWebMercator = fromLonLat(washingtonLonLat); const map = new Map({ layers: [ new TileLayer({ source: new OSM() }) ], target: 'map', view: new View({ center: washingtonWebMercator, zoom: 8 }) }); ``` -------------------------------- ### Basic Map Setup with Different Source and View Projections Source: https://openlayers.org/doc/tutorials/raster-reprojection.html Configure a map with a TileWMS layer and a view, specifying different projections for the source and the view. OpenLayers will automatically reproject the raster data. ```javascript import Map from 'ol/Map.js'; import TileLayer from 'ol/layer/Tile.js'; import TileWMS from 'ol/source/TileWMS.js'; import View from 'ol/View.js'; const map = new Map({ target: 'map', view: new View({ projection: 'EPSG:3857', // here is the view projection center: [0, 0], zoom: 2, }), layers: [ new TileLayer({ source: new TileWMS({ projection: 'EPSG:4326', // here is the source projection url: 'https://ahocevar.com/geoserver/wms', params: { 'LAYERS': 'ne:NE1_HR_LC_SR_W_DR', }, }), }), ], }); ``` -------------------------------- ### Center Map on Specific Coordinates (Correct Order) Source: https://openlayers.org/doc/faq.html Shows how to correctly center a map on Schladming by providing coordinates in the longitude, latitude order. This example highlights the importance of the correct order for `fromLonLat` when using EPSG:4326. ```javascript import Map from 'ol/Map.js'; import View from 'ol/View.js'; import TileLayer from 'ol/layer/Tile.js'; import OSM from 'ol/source/OSM.js'; import {fromLonLat} from 'ol/proj.js'; const schladming = [13.689167, 47.394167]; // longitude first, then latitude // since we are using OSM, we have to transform the coordinates... const schladmingWebMercator = fromLonLat(schladming); const map = new Map({ layers: [ new TileLayer({ source: new OSM() }) ], target: 'map', view: new View({ center: schladmingWebMercator, zoom: 9 }) }); ``` -------------------------------- ### Initialize Map with Incorrect Coordinates Source: https://openlayers.org/doc/faq.html This example shows how providing longitude and latitude directly to the map view center can lead to unexpected centering if the coordinates are not in the map's default projection (Web Mercator). ```javascript import Map from 'ol/Map.js'; import View from 'ol/View.js'; import TileLayer from 'ol/layer/Tile.js'; import OSM from 'ol/source/OSM.js'; const washingtonLonLat = [-77.036667, 38.895]; const map = new Map({ layers: [ new TileLayer({ source: new OSM() }) ], target: 'map', view: new View({ center: washingtonLonLat, zoom: 12 }) }); ``` -------------------------------- ### Get Feature Count from KML Source (Asynchronous) Source: https://openlayers.org/doc/faq.html Attempts to get the number of features from a KML source immediately after layer creation. This will initially log 0 because KML loading is asynchronous. Use event listeners for accurate counts after loading. ```javascript import VectorLayer from 'ol/layer/Vector.js'; import KMLSource from 'ol/source/KML.js'; const vector = new VectorLayer({ source: new KMLSource({ projection: 'EPSG:3857', url: 'data/kml/2012-02-10.kml' }) }); const numFeatures = vector.getSource().getFeatures().length; console.log("Count right after construction: " + numFeatures); ``` -------------------------------- ### Build OpenLayers Application for Deployment Source: https://openlayers.org/doc/quickstart.html Run `npm run build` to bundle your application into a `dist` directory, creating static files ready for production deployment. ```bash npm run build ``` -------------------------------- ### Create OpenStreetMap Source Source: https://openlayers.org/doc/tutorials/concepts.html Instantiates an OpenStreetMap source, which provides tiled map data. This is a common source for base maps. ```javascript import OSM from 'ol/source/OSM.js'; const source = new OSM(); ``` -------------------------------- ### Import Map and View Classes Source: https://openlayers.org/doc/tutorials/background.html Import Map and View classes from the 'ol/Map.js' and 'ol/View.js' modules respectively. This is the standard way to import classes that are default exports. ```javascript import Map from 'ol/Map.js'; import View from 'ol/View.js'; ``` -------------------------------- ### Import Utility Functions Source: https://openlayers.org/doc/tutorials/background.html Import utility functions like getUid from 'ol' and fromLonLat from 'ol/proj.js'. This shows how to import named exports for constants and functions from lowercase modules. ```javascript import {getUid} from 'ol'; import {fromLonLat} from 'ol/proj.js'; ``` -------------------------------- ### Initialize OpenLayers Map Source: https://openlayers.org/doc/tutorials/concepts.html Constructs a new OpenLayers Map instance and renders it into the specified target div. Ensure the target div exists in your HTML. ```javascript import Map from 'ol/Map.js'; const map = new Map({target: 'map'}); ``` -------------------------------- ### Import Layer Classes Source: https://openlayers.org/doc/tutorials/background.html Import Map and View classes directly from the 'ol' package, and Tile and Vector layer classes from 'ol/layer.js'. This demonstrates convenience re-exports for commonly used classes. ```javascript import {Map, View} from 'ol'; import {Tile, Vector} from 'ol/layer.js'; ``` -------------------------------- ### Listen for KML Source Load Completion Source: https://openlayers.org/doc/faq.html Attaches an event listener to the KML source to detect when the data has finished loading and is ready. It then logs the accurate number of features available in the source. ```javascript vector.getSource().on('change', function(evt){ const source = evt.target; if (source.getState() === 'ready') { const numFeatures = source.getFeatures().length; console.log("Count after change: " + numFeatures); } }); ``` -------------------------------- ### Initialize OpenLayers Map in JavaScript Source: https://openlayers.org/doc/quickstart.html This script initializes a new OpenLayers map with an OSM tile layer and a default view. It imports necessary modules and sets the map target, layers, and view. ```javascript 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'; const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: [0, 0], zoom: 2, }), }); ``` -------------------------------- ### Load KML Features into a Vector Layer Source: https://openlayers.org/doc/faq.html Initializes a VectorLayer to load features from a KML file. The `projection` is set to 'EPSG:3857' for display, and the `url` points to the KML data. ```javascript import VectorLayer from 'ol/layer/Vector.js'; import KMLSource from 'ol/source/KML.js'; const vector = new VectorLayer({ source: new KMLSource({ projection: 'EPSG:3857', url: 'data/kml/2012-02-10.kml' }) }); ``` -------------------------------- ### Add Tile Layer to Map Source: https://openlayers.org/doc/tutorials/concepts.html Creates a TileLayer using a previously defined source and adds it to the map. This layer will display the map tiles. ```javascript import TileLayer from 'ol/layer/Tile.js'; // ... const layer = new TileLayer({source: source}); map.addLayer(layer); ``` -------------------------------- ### Basic HTML Structure for OpenLayers Map Source: https://openlayers.org/doc/quickstart.html The HTML file requires a `div` element to contain the map and a script tag to load the JavaScript. ```html