@@ -50,10 +50,56 @@ $(document).ready(function() {
50
50
center : [ - 75.158924 , 39.9629223 ] ,
51
51
zoom : 11
52
52
} ) ;
53
+
54
+ // Load Mapbox Geocoder
55
+ const geocoder = new MapboxGeocoder ( {
56
+ accessToken : mapboxgl . accessToken ,
57
+ mapboxgl : mapboxgl , // Set the mapbox-gl instance
58
+ marker : true , // Use the geocoder's default marker style
59
+ } ) ;
60
+
61
+ // Add Geocoder to map
62
+ map . addControl ( geocoder , 'top-right' ) ;
63
+
64
+ // Event listener for geocoder completion
65
+ geocoder . on ( 'result' , ( { result } ) => {
66
+ const searchResult = result . geometry ;
67
+ const options = { units : 'miles' } ;
68
+
69
+ // Loop through sites and calculate distance to geocoder address
70
+ for ( const loc of data . loc ) {
71
+ loc . distance = turf . distance (
72
+ searchResult ,
73
+ [ loc . longitude , loc . latitude ] ,
74
+ options
75
+ ) ;
76
+ }
77
+
78
+ // find site with min distance
79
+ const closest = data . loc . sort ( ( a , b ) => {
80
+ if ( a . distance > b . distance ) {
81
+ return 1 ;
82
+ }
83
+ if ( a . distance < b . distance ) {
84
+ return - 1 ;
85
+ }
86
+ return 0 ; // a must be equal to b
87
+ } ) [ 0 ] ;
88
+
89
+ // fit the map on the entered location and the closest site
90
+ map . fitBounds ( [ searchResult . coordinates , [ closest . longitude , closest . latitude ] ] , { padding : 200 } ) ;
91
+
92
+ // Load popup of closest location
93
+ link_object = window . location . origin + "/table/" + table_name + "/oid=" + closest . oid + "/" ;
94
+ const popup = new mapboxgl . Popup ( )
95
+ . setLngLat ( [ closest . longitude , closest . latitude ] )
96
+ . setHTML ( "<a href=" + link_object + ">" + JSON . stringify ( closest . name1 ) + "</a>" )
97
+ . addTo ( map ) ;
98
+ } ) ;
99
+
53
100
document . querySelector ( "#sitecount" ) . textContent = data [ 'loc' ] . length
54
101
var link_object ;
55
102
for ( i = 0 ; i < data [ 'loc' ] . length ; i ++ ) {
56
- console . log ( [ data [ 'loc' ] [ i ] [ 'latitude' ] , data [ 'loc' ] [ i ] [ 'longitude' ] ] )
57
103
link_object = window . location . origin + "/table/" + table_name + "/oid=" + data [ 'loc' ] [ i ] [ 'oid' ] + "/" ;
58
104
var marker = new mapboxgl . Marker ( )
59
105
. setLngLat ( [ data [ 'loc' ] [ i ] [ 'longitude' ] , data [ 'loc' ] [ i ] [ 'latitude' ] ] )
0 commit comments