OpenLayers Examples
22An enterprise-grade GIS library with advanced projections and formats. All examples use MapTiler Cloud tiles.
Map display & interaction
Satellite Basemap with OpenLayers Load MapTiler satellite imagery into OpenLayers as an ol.layer.Tile backed by an ol.source.XYZ source, with an ol.View zoomed in on Paris.
Configure Interactions in OpenLayers Enable or disable OpenLayers map interactions such as wheel zoom, drag pan, and pinch rotate by passing options to ol.interaction.defaults.defaults.
Initialize an OpenLayers Map Create an OpenLayers map from a MapTiler vector style with olms.apply() from ol-mapbox-style, then set its center and zoom through getView().
Switch Vector Styles in OpenLayers Switch an OpenLayers map between MapTiler vector styles by reloading it with olms.apply() while keeping the center and zoom read from getView().
Static Map Image with OpenLayers Request a MapTiler Static Maps API image as a plain img element and show it next to an interactive OpenLayers ol.Map of the same Berlin view.
Markers, annotations & overlays
Custom Marker Icons with OpenLayers Replace default OpenLayers point styling with a custom SVG pin by giving each ol.Feature an ol.style.Icon inside a per-feature ol.style.Style.
Choropleth Map with OpenLayers Color GeoJSON polygons by value in OpenLayers with a style function on an ol.layer.Vector that returns an ol.style.Style with a data-driven ol.style.Fill.
LineString Features in OpenLayers Add a line to an OpenLayers map by placing an ol.geom.LineString feature in an ol.source.Vector and styling it with an ol.style.Stroke.
Polygon Features in OpenLayers Render a filled area in OpenLayers by adding an ol.geom.Polygon feature to an ol.layer.Vector, styled with ol.style.Fill and ol.style.Stroke.
Add Markers and Popups in OpenLayers Place clickable markers in OpenLayers as ol.geom.Point features on an ol.layer.Vector and open a popup with ol.Overlay found by forEachFeatureAtPixel.
Point Clustering with OpenLayers Cluster nearby points in OpenLayers with ol.source.Cluster, sizing each ol.style.Circle by member count and labeling it with ol.style.Text.
Data-Driven Styling with OpenLayers Style OpenLayers features from their data by returning an ol.style.Style per feature, with circle color by category and radius by rating.
Geocoding & search
Address Search with OpenLayers Geocode an address with the MapTiler Geocoding API via fetch(), then place the result as an ol.Feature in OpenLayers and animate the ol.View to it.
Reverse Geocoding with OpenLayers Reverse geocode a clicked spot in OpenLayers by converting it with ol.proj.toLonLat, querying the MapTiler Geocoding API, and showing it in an ol.Overlay.
Routing & navigation
Location & real-time
Geofencing with OpenLayers Check whether a clicked point lies inside a geofence in OpenLayers by testing an ol.geom.Polygon boundary with intersectsCoordinate().
Geolocation with OpenLayers Follow the user's position in OpenLayers with ol.Geolocation, drawing the location and its accuracy circle as features on an ol.layer.Vector.
Analytics & advanced
Draw Interaction in OpenLayers Draw points, lines, and polygons in OpenLayers with ol.interaction.Draw writing to an ol.source.Vector, and measure areas with ol.sphere.getArea.
Export Map as PNG with OpenLayers Export an OpenLayers map to PNG by waiting for the rendercomplete event, calling renderSync(), and reading the canvas with toDataURL().
Heatmap Layer with OpenLayers Visualize point density in OpenLayers with ol.layer.Heatmap, using a weight function on ol.Feature points from an ol.source.Vector over a raster basemap.
Load GeoJSON in OpenLayers Parse GeoJSON in OpenLayers with ol.format.GeoJSON readFeatures() and render the features on an ol.layer.Vector styled per geometry type.
Store Locator with OpenLayers Build a store locator in OpenLayers with a sidebar list linked to ol.Feature points, an ol.Overlay popup, and getView().animate() on selection.
Build maps using OpenLayers with AI
Install the OpenLayers skill in your IDE and let AI write the code for you.
All examples use MapTiler maps and require a free API key.