1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import {Feature, Map, Overlay, View} from 'ol/index.js'; import {OSM, Vector as VectorSource} from 'ol/source.js'; import {Point} from 'ol/geom.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js'; import {useGeographic} from 'ol/proj.js';
useGeographic();
const place = [-110, 45];
const point = new Point(place);
const map = new Map({ target: 'map', view: new View({ center: place, zoom: 8, }), layers: [ new TileLayer({ source: new OSM(), }), new VectorLayer({ source: new VectorSource({ features: [new Feature(point)], }), style: { 'circle-radius': 9, 'circle-fill-color': 'red', }, }), ], });
const element = document.getElementById('popup');
const popup = new Overlay({ element: element, stopEvent: false, }); map.addOverlay(popup);
function formatCoordinate(coordinate) { return ` <table> <tbody> <tr><th>lon</th><td>${coordinate[0].toFixed(2)}</td></tr> <tr><th>lat</th><td>${coordinate[1].toFixed(2)}</td></tr> </tbody> </table>`; }
const info = document.getElementById('info'); map.on('moveend', function () { const view = map.getView(); const center = view.getCenter(); info.innerHTML = formatCoordinate(center); });
let popover; map.on('click', function (event) { if (popover) { popover.dispose(); popover = undefined; } const feature = map.getFeaturesAtPixel(event.pixel)[0]; if (!feature) { return; } const coordinate = feature.getGeometry().getCoordinates(); popup.setPosition([ coordinate[0] + Math.round(event.coordinate[0] / 360) * 360, coordinate[1], ]);
popover = new bootstrap.Popover(element, { container: element.parentElement, content: formatCoordinate(coordinate), html: true, offset: [0, 20], placement: 'top', sanitize: false, }); popover.show(); });
map.on('pointermove', function (event) { const type = map.hasFeatureAtPixel(event.pixel) ? 'pointer' : 'inherit'; map.getViewport().style.cursor = type; });
|