Using geographic coordinates for the map view.
Calling the useGeographic
function in the 'ol/proj'
module makes it so the map view uses geographic coordinates (even if the view projection is not geographic).
<!DOCTYPE html>
<html lang="en">
<head>
<title>Geographic Coordinates</title>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
.map {
width: 100%;
height:400px;
}
td {
padding: 0 0.5em;
text-align: right;
}
</style>
</head>
<body>
<div id="map" class="map"><div id="popup"></div></div>
<div id="info"></div>
<script src="index.js"></script>
</body>
</html>
import 'ol/ol.css';
import {useGeographic} from 'ol/proj';
import {Map, View, Feature, Overlay} from 'ol/index';
import {Point} from 'ol/geom';
import {Vector as VectorLayer, Tile as TileLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Style, Circle, Fill} from 'ol/style';
useGeographic();
var place = [-110, 45];
var point = new Point(place);
var 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: new Style({
image: new Circle({
radius: 9,
fill: new Fill({color: 'red'})
})
})
})
]
});
var element = document.getElementById('popup');
var popup = new Overlay({
element: element,
positioning: 'bottom-center',
stopEvent: false,
offset: [0, -10]
});
map.addOverlay(popup);
function formatCoordinate(coordinate) {
return ("\n <table>\n <tbody>\n <tr><th>lon</th><td>" + (coordinate[0].toFixed(2)) + "</td></tr>\n <tr><th>lat</th><td>" + (coordinate[1].toFixed(2)) + "</td></tr>\n </tbody>\n </table>");
}
var info = document.getElementById('info');
map.on('moveend', function() {
var view = map.getView();
var center = view.getCenter();
info.innerHTML = formatCoordinate(center);
});
map.on('click', function(event) {
var feature = map.getFeaturesAtPixel(event.pixel)[0];
if (feature) {
var coordinate = feature.getGeometry().getCoordinates();
popup.setPosition(coordinate);
$(element).popover({
placement: 'top',
html: true,
content: formatCoordinate(coordinate)
});
$(element).popover('show');
} else {
$(element).popover('destroy');
}
});
map.on('pointermove', function(event) {
if (map.hasFeatureAtPixel(event.pixel)) {
map.getViewport().style.cursor = 'pointer';
} else {
map.getViewport().style.cursor = 'inherit';
}
});
{
"name": "geographic",
"dependencies": {
"ol": "6.1.1"
},
"devDependencies": {
"parcel": "1.11.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --experimental-scope-hoisting --public-url . index.html"
}
}