Skip to content

Commit b27cb9c

Browse files
committed
Determine initial municipality by visitors IP
1 parent 257025c commit b27cb9c

File tree

2 files changed

+95
-10
lines changed

2 files changed

+95
-10
lines changed

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# Recent verleende vergunningen op de kaart
1+
# Bouwmelding.nl: vergunningen op de kaart
22

3-
Bekijk vergunningen aangevraagd en verleend in uw omgeving.
4-
5-
<https://basgroot.github.io/bekendmakingen/>
3+
Bekijk vergunningen aangevraagd en verleend in uw omgeving. Demo op <https://bouwmelding.nl/>.
64

75
## Legenda
86

@@ -20,7 +18,3 @@ Bekijk vergunningen aangevraagd en verleend in uw omgeving.
2018
- <img src="img/verkeer.svg" alt="Verkeersbesluit" width="40"/> Verkeersbesluit.
2119
- <img src="img/constructie.svg" alt="Bouwvergunning" width="40"/> Bouwvergunning.
2220
- <img src="img/wetboek.svg" alt="Wetboek" width="40"/> De rest. Meldingen, verordeningen en overige besluiten.
23-
24-
Deze website maakt gebruik van de [Open Overheid](https://data.overheid.nl/dataset/officiele-bekendmakingen).
25-
26-
Zie verder de [SRU-standaard](https://www.loc.gov/standards/sru/sru-2-0.html).

map.js

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ function initMap() {
788788
}
789789
}
790790

791+
/*
792+
* Add municipalyty and other parameters to the URL, so your view can be shared.
793+
*/
791794
function updateUrl(zoom, center) {
792795
// Add to URL: /?in=Alkmaar&zoom=15&center=52.43660651356703,4.84418395002761
793796
if (window.URLSearchParams) {
@@ -800,6 +803,94 @@ function initMap() {
800803
document.title = "Bekendmakingen " + appState.activeMunicipality;
801804
}
802805

806+
/*
807+
* Calculate the distance between two points, using the haversine formula.
808+
*/
809+
function computeDistanceBetween(from, to) {
810+
// Source: http://www.movable-type.co.uk/scripts/latlong.html
811+
// Maps API covers this function as well:
812+
// https://developers.google.com/maps/documentation/javascript/reference/geometry#spherical.computeDistanceBetween
813+
const radius = 6371e3; // metres
814+
const a1 = from.lat * Math.PI / 180; // φ1: φ, λ in radians
815+
const a2 = to.lat * Math.PI / 180; // φ2
816+
const latDelta = (to.lat - from.lat) * Math.PI / 180; // Δφ
817+
const lngDelta = (to.lng - from.lng) * Math.PI / 180; // Δλ
818+
const a = Math.sin(latDelta / 2) * Math.sin(latDelta / 2) + Math.cos(a1) * Math.cos(a2) * Math.sin(lngDelta / 2) * Math.sin(lngDelta / 2);
819+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
820+
return radius * c; // Distance in metres
821+
}
822+
823+
/*
824+
* Not accurate, but try to find the closest municipality center.
825+
*/
826+
function setClosestMunicipality(position) {
827+
const municipalityNames = Object.keys(municipalities);
828+
var distance = 1000000;
829+
municipalityNames.forEach(function (municipalityName) {
830+
const municipalityObject = municipalities[municipalityName];
831+
const distanceBetweenMunicipalityAndViewer = computeDistanceBetween(position, municipalityObject.center) / 1000;
832+
if (distanceBetweenMunicipalityAndViewer < distance) {
833+
console.log("Found closer municipality: " + municipalityName);
834+
distance = distanceBetweenMunicipalityAndViewer;
835+
appState.activeMunicipality = municipalityName;
836+
}
837+
});
838+
}
839+
840+
/*
841+
* Determine if the municipality is part of the URL.
842+
*/
843+
function isLocationInUrl() {
844+
var municipality;
845+
if (window.URLSearchParams) {
846+
urlParams = new window.URLSearchParams(window.location.search);
847+
municipality = urlParams.get("in");
848+
if (municipality && municipalities[municipality] !== undefined) {
849+
return true;
850+
}
851+
}
852+
return false;
853+
}
854+
855+
/*
856+
* Try to find the municipality of the visitor, by using an IP geolocation API.
857+
*/
858+
function getLocationAndLoadData() {
859+
if (isLocationInUrl()) {
860+
internalInitMap();
861+
return; // The location is explicitly requested. Don't adapt location based on visitors IP address.
862+
}
863+
fetch(
864+
"https://basement.nl/proxy-server/location.php",
865+
{
866+
"method": "GET"
867+
}
868+
).then(function (response) {
869+
if (response.ok) {
870+
response.json().then(function (responseJson) {
871+
if (municipalities[responseJson.city] !== undefined) {
872+
// Name of the city is the same as the municipality.
873+
console.log("Client location in municipality " + responseJson.city);
874+
appState.activeMunicipality = responseJson.city;
875+
} else {
876+
// Try to locate the closest municipality:
877+
setClosestMunicipality({
878+
"lat": responseJson.lat,
879+
"lng": responseJson.lng
880+
});
881+
}
882+
internalInitMap();
883+
});
884+
} else {
885+
console.error(response);
886+
internalInitMap();
887+
}
888+
}).catch(function (error) {
889+
console.error(error);
890+
internalInitMap();
891+
});
892+
}
893+
803894
function internalInitMap() {
804895
var containerElm = document.getElementById("map");
805896
var mapSettings = getInitialMapSettings();
@@ -862,6 +953,7 @@ function initMap() {
862953
updateUrl(appState.map.getZoom(), appState.map.getCenter());
863954
console.log("Remaining items to add to the map: " + appState.delayedMarkersArray.length);
864955
});
956+
loadData();
865957
}
866958

867959
function navigateTo(municipality) {
@@ -1043,6 +1135,5 @@ function initMap() {
10431135
loadDataForMunicipality(appState.activeMunicipality, 1);
10441136
}
10451137

1046-
internalInitMap();
1047-
loadData();
1138+
getLocationAndLoadData();
10481139
}

0 commit comments

Comments
 (0)