OpenStreetMap Integration in Portal with Leaflet

How to integrate OpenStreetMap to the WEBCON portal easily with Leaflet to gain functionality in processes for e.g. point of interests search, navigation, display in maps.

OpenStreetMap Integration in Portal with Leaflet

If you need functionality in processes for e.g. point of interests search, navigation, display in maps data from OpenStreetMap is very userful. The integration to the WEBCON portal can be easily realised with Leaflet. This software is the leading open-source JavaScript library for mobile-friendly interactive maps.

Form fields

Create all needed form fields for the address data and set the following form rule in the form field “POI”:

HTML form field

The integration in WEBCON is done with a HTML form field containing this code:

HTML
<!-- Container for the map -->
<div id="map-container" style="width: 100%; height: 300px;"></div>
<!-- Load leaflet CSS & JS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>

Form Rule

Create a local or a global (with parameters) form rule “OSM Integration” with “JavaScript mode” as “edit mode” with the following javascript code. Just chose your form fields or parameters in the list to put them in the javascript code section where the WEBCON functions “GetValue” and “SetValue” are used. Add this form rule in the tab “Style and behavior” of the form search field “POI” as shown before.

Screenshot

JS Code

JavaScript
let poiMap = null;
let lastMarker = null;
(() => {
// Reset address form fields
SetValue("#{FLD:2779}#", "");
SetValue("#{FLD:2781}#", "");
SetValue("#{FLD:2778}#", "");
SetValue("#{FLD:2776}#", "");
SetValue("#{FLD:2855}#", "");
SetValue("#{FLD:2777}#", "");
SetValue("#{FLD:2782}#", "");
SetValue("#{FLD:2856}#", "");
SetValue("#{FLD:2857}#", "");
let poiValue = GetValue("#{FLD:2773}#");
if (!poiValue || poiValue == "" || typeof poiValue === 'undefined') {
//console.log("poiValue is undefined or empty!");
return;
}
function updateMap(lat, lon, label) {
const mapContainer = document.getElementById("map-container");
if (!mapContainer || typeof L === 'undefined') {
//console.log("mapContainer is undefined!");
return;
}
// Initialize map if there is none
if (!mapContainer._leaflet_id) {
poiMap = L.map(mapContainer).setView([lat, lon], 13);
//console.log(poiMap);
mapContainer._leaflet_map = poiMap; // merken!
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; OpenStreetMap contributors'
}).addTo(poiMap);
} else {
poiMap = mapContainer._leaflet_map;
//console.log(poiMap);
poiMap.setView([lat, lon], 13);
}
// Renew marker
if (lastMarker) {
poiMap.removeLayer(lastMarker);
}
lastMarker = L.marker([lat, lon]).addTo(poiMap).bindPopup(label).openPopup();
}
let query = encodeURIComponent(poiValue);
//console.log(query);
let url = `https://nominatim.openstreetmap.org/search?format=json&accept-language=de&addressdetails=1&limit=1&q=${query}`;
//console.log(url);
fetch(url)
.then(response => response.json())
.then(data => {
if (data.length > 0) {
let result = data[0];
//console.log(result);
// Set address fields
SetValue("#{FLD:2779}#", result.address.road || "");
SetValue("#{FLD:2781}#", result.address.house_number|| "");
SetValue("#{FLD:2778}#", result.address.postcode || "");
SetValue("#{FLD:2776}#", result.address.city|| "");
SetValue("#{FLD:2855}#", result.address.city|| "");
SetValue("#{FLD:2777}#", result.address.state|| "");
SetValue("#{FLD:2782}#", result.address.country|| "");
SetValue("#{FLD:2856}#", result.lat || "");
SetValue("#{FLD:2857}#", result.lon || "");
// Update map
updateMap(result.lat, result.lon, result.display_name);
} else {
alert("No search results found!");
}
});
})();

The language of the result data (e.g. coutry, state, city) can be set with the parameter “accept-language” in the query url. Be aware of the correct quotes (backtick).

let url = https://nominatim.openstreetmap.org/search?format=json&accept-language=de&addressdetails=1&limit=1&q=${query};

JSON result object

This is the returned object for the query “Schloss Karlsruhe”:

JSON
{
"place_id": 112819066,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
"osm_type": "way",
"osm_id": 316433983,
"lat": "49.0135248",
"lon": "8.4043592",
"class": "historic",
"type": "castle",
"place_rank": 30,
"importance": 0.3751571465506199,
"addresstype": "historic",
"name": "Schloss Karlsruhe",
"display_name": "Schloss Karlsruhe, 1, Schlossbezirk, Innenstadt-West Östlicher Teil, Innenstadt-West, Karlsruhe, Baden-Württemberg, 76131, Deutschland",
"address": {
"historic": "Schloss Karlsruhe",
"house_number": "1",
"road": "Schlossbezirk",
"neighbourhood": "Innenstadt-West Östlicher Teil",
"suburb": "Innenstadt-West",
"city": "Karlsruhe",
"state": "Baden-Württemberg",
"ISO3166-2-lvl4": "DE-BW",
"postcode": "76131",
"country": "Deutschland",
"country_code": "de"
},
"boundingbox": [
"49.0130412",
"49.0140237",
"8.4032308",
"8.4054431"
]
}

Screenshot of the form in the portal

Categories

Comments

Comments will be added here later.