@@ -788,6 +788,9 @@ function initMap() {
788
788
}
789
789
}
790
790
791
+ /*
792
+ * Add municipalyty and other parameters to the URL, so your view can be shared.
793
+ */
791
794
function updateUrl ( zoom , center ) {
792
795
// Add to URL: /?in=Alkmaar&zoom=15¢er=52.43660651356703,4.84418395002761
793
796
if ( window . URLSearchParams ) {
@@ -800,6 +803,94 @@ function initMap() {
800
803
document . title = "Bekendmakingen " + appState . activeMunicipality ;
801
804
}
802
805
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
+
803
894
function internalInitMap ( ) {
804
895
var containerElm = document . getElementById ( "map" ) ;
805
896
var mapSettings = getInitialMapSettings ( ) ;
@@ -862,6 +953,7 @@ function initMap() {
862
953
updateUrl ( appState . map . getZoom ( ) , appState . map . getCenter ( ) ) ;
863
954
console . log ( "Remaining items to add to the map: " + appState . delayedMarkersArray . length ) ;
864
955
} ) ;
956
+ loadData ( ) ;
865
957
}
866
958
867
959
function navigateTo ( municipality ) {
@@ -1043,6 +1135,5 @@ function initMap() {
1043
1135
loadDataForMunicipality ( appState . activeMunicipality , 1 ) ;
1044
1136
}
1045
1137
1046
- internalInitMap ( ) ;
1047
- loadData ( ) ;
1138
+ getLocationAndLoadData ( ) ;
1048
1139
}
0 commit comments