This updates ESLint and our shared eslint-config-openlayers to use Prettier. Most formatting changes were automatically applied with this:
npm run lint -- --fix
A few manual changes were required:
* In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
* In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason. While editing this, I reworked `ExampleBuilder` to be a class.
* In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
import Map from '../src/ol/Map.js';
|
|
import Overlay from '../src/ol/Overlay.js';
|
|
import TileJSON from '../src/ol/source/TileJSON.js';
|
|
import TileLayer from '../src/ol/layer/Tile.js';
|
|
import UTFGrid from '../src/ol/source/UTFGrid.js';
|
|
import View from '../src/ol/View.js';
|
|
|
|
const key =
|
|
'pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiY2pzbmg0Nmk5MGF5NzQzbzRnbDNoeHJrbiJ9.7_-_gL8ur7ZtEiNwRfCy7Q';
|
|
|
|
const mapLayer = new TileLayer({
|
|
source: new TileJSON({
|
|
url:
|
|
'https://api.tiles.mapbox.com/v4/mapbox.geography-class.json?secure&access_token=' +
|
|
key,
|
|
}),
|
|
});
|
|
|
|
const gridSource = new UTFGrid({
|
|
url:
|
|
'https://api.tiles.mapbox.com/v4/mapbox.geography-class.json?secure&access_token=' +
|
|
key,
|
|
});
|
|
|
|
const gridLayer = new TileLayer({source: gridSource});
|
|
|
|
const view = new View({
|
|
center: [0, 0],
|
|
zoom: 1,
|
|
});
|
|
|
|
const mapElement = document.getElementById('map');
|
|
const map = new Map({
|
|
layers: [mapLayer, gridLayer],
|
|
target: mapElement,
|
|
view: view,
|
|
});
|
|
|
|
const infoElement = document.getElementById('country-info');
|
|
const flagElement = document.getElementById('country-flag');
|
|
const nameElement = document.getElementById('country-name');
|
|
|
|
const infoOverlay = new Overlay({
|
|
element: infoElement,
|
|
offset: [15, 15],
|
|
stopEvent: false,
|
|
});
|
|
map.addOverlay(infoOverlay);
|
|
|
|
const displayCountryInfo = function (coordinate) {
|
|
const viewResolution = /** @type {number} */ (view.getResolution());
|
|
gridSource.forDataAtCoordinateAndResolution(
|
|
coordinate,
|
|
viewResolution,
|
|
function (data) {
|
|
// If you want to use the template from the TileJSON,
|
|
// load the mustache.js library separately and call
|
|
// info.innerHTML = Mustache.render(gridSource.getTemplate(), data);
|
|
mapElement.style.cursor = data ? 'pointer' : '';
|
|
if (data) {
|
|
flagElement.src = 'data:image/png;base64,' + data['flag_png'];
|
|
nameElement.innerHTML = data['admin'];
|
|
}
|
|
infoOverlay.setPosition(data ? coordinate : undefined);
|
|
}
|
|
);
|
|
};
|
|
|
|
map.on('pointermove', function (evt) {
|
|
if (evt.dragging) {
|
|
return;
|
|
}
|
|
const coordinate = map.getEventCoordinate(evt.originalEvent);
|
|
displayCountryInfo(coordinate);
|
|
});
|
|
|
|
map.on('click', function (evt) {
|
|
displayCountryInfo(evt.coordinate);
|
|
});
|