diff --git a/src/assets/static/js/pages/ui-map-openlayers.js b/src/assets/static/js/pages/ui-map-openlayers.js new file mode 100644 index 000000000..39cbeb8ad --- /dev/null +++ b/src/assets/static/js/pages/ui-map-openlayers.js @@ -0,0 +1,76 @@ +// init map +var map = new ol.Map({ + target: 'map', + layers: [ + new ol.layer.Tile({ + source: new ol.source.OSM() + }) + ], + view: new ol.View({ + center: ol.proj.fromLonLat([110.3803253, -7.7945047]), + zoom: 12 + }) +}); + +// add vector layer +var vectorLayer = new ol.layer.Vector({ + source: new ol.source.Vector({ + features: [ + new ol.Feature({ + geometry: new ol.geom.Point(ol.proj.fromLonLat([110.3647631, -7.8013849])), + name: 'Yogyakarta, Indonesia' + }) + ] + }) +}); + +// set style for vector layer +vectorLayer.setStyle(new ol.style.Style({ + image: new ol.style.Icon({ + anchor: [0.5, 46], + anchorXUnits: 'fraction', + anchorYUnits: 'pixels', + src: 'https://openlayers.org/en/latest/examples/data/icon.png' + }) +})); + +map.addLayer(vectorLayer); + +// popup +const element = document.getElementById('popup'); + +const popup = new ol.Overlay({ + element: element, + positioning: 'bottom-center', + stopEvent: false, +}); +map.addOverlay(popup); + +let popover; +function disposePopover() { + if (popover) { + popover.dispose(); + popover = undefined; + } +} + +// display popup on click +map.on('click', function (evt) { + const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { + return feature; + }); + disposePopover(); + if (!feature) { + return; + } + popup.setPosition(evt.coordinate); + popover = new bootstrap.Popover(element, { + placement: 'top', + html: true, + content: feature.get('name'), + }); + popover.show(); +}); + +// scale line +map.addControl(new ol.control.ScaleLine()); \ No newline at end of file diff --git a/src/sidebar-items.json b/src/sidebar-items.json index fa13fe5b6..f3c7649a4 100644 --- a/src/sidebar-items.json +++ b/src/sidebar-items.json @@ -326,6 +326,10 @@ { "name": "Leaflet Map", "url": "ui-map-leaflet.html" + }, + { + "name": "OpenLayers Map", + "url": "ui-map-openlayers.html" } ] }, diff --git a/src/ui-map-openlayers.html b/src/ui-map-openlayers.html new file mode 100644 index 000000000..b3a2c3349 --- /dev/null +++ b/src/ui-map-openlayers.html @@ -0,0 +1,51 @@ +{% set title = 'OpenLayers Map' %} +{% set filename = 'ui-map-openlayers.html' %} + +{% extends 'src/layouts/master.html' %} +{% block content %} +
A high-performance, feature-packed library for all your mapping needs.
+