Getting the longitude and latitude from the browser or by entering the data manually in the form.
- Using the OpenLayers to display the location on a map.
- Using OpenStreetMap to display the map
- Using Nominatim to get the address information
When you “Reverse Geocode" the longitude and latitude
- adresinfo is shown below
- the map is zoomed to the adres
Address information
You can also click on the map to display address information for a specific location.
- Ideas taken from: Mrunay Uttarwar and Simple OpenLayers Reverse Geocoding sample with Nominatim
- Documentation Geolocation API
- Browser compatibility
Source
The OpenLayers JS and CSS in the <head>
section of the page:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@v7.1.0/dist/ol.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v7.1.0/ol.css">
The javascript
var lat = document.getElementById("lat");
var lon = document.getElementById("lon");
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
function simpleReverseGeocoding(lon, lat) {
fetch('https://nominatim.openstreetmap.org/reverse?format=json&lon=' + lon + '&lat=' + lat)
.then(function(response) {
if(!response.ok) {
throw new Error("Response not ok");
}
return response.json();
}).then(function(json) {
document.getElementById('address').innerHTML = json.display_name;
document.getElementById('address').style.visibility = 'visible';
document.getElementById('address-information').style.visibility = 'visible';
map.getView().setCenter(ol.proj.transform([lon, lat], 'EPSG:4326', 'EPSG:3857'));
map.getView().setZoom(18);
})
.catch((error) => {
console.error('There was an error with the fetch operation: ', error);
})
}
map.on('click', function(e) {
var coordinate = ol.proj.toLonLat(e.coordinate).map(function(val) {
return val.toFixed(6);
});
var lon = document.getElementById('lon').value = coordinate[0];
var lat = document.getElementById('lat').value = coordinate[1];
simpleReverseGeocoding(lon, lat);
});
document.getElementById('reversegeocoding').addEventListener('click', function(e) {
if (document.getElementById('lon').value && document.getElementById('lat').value) {
simpleReverseGeocoding(document.getElementById('lon').value, document.getElementById('lat').value);
}
});
document.getElementById('zoom-out').onclick = function () {
const view = map.getView();
const zoom = view.getZoom();
view.setZoom(zoom - 1);
};
document.getElementById('zoom-in').onclick = function () {
const view = map.getView();
const zoom = view.getZoom();
view.setZoom(zoom + 1);
};
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
lat.value = position.coords.latitude;
lon.value = position.coords.longitude;
}
getLocation();
- Previous: Full screen API