@@ -16,9 +16,28 @@ function init()
1616 const property_id = form . querySelector ( '#property_id' ) ;
1717 console . log ( 'User entered:' , token . value , property_id . value ) ;
1818
19- // create map and zoom in on farm
20- // TODO: can we get extents programmatically?
21- let map = L . map ( 'map' ) . setView ( [ - 31.349912533186938 , 150.13404649761717 ] , 14 ) ;
19+ // create map
20+ let map = L . map ( 'map' )
21+
22+ // get the bounds of the entered farm
23+ fetch ( g_tileurl + '/getbounds/' + property_id . value , {
24+ headers : {
25+ 'Authorization' : 'Bearer ' + token . value ,
26+ 'Accept' : 'application/json'
27+ } ,
28+ method : "GET" } )
29+ . then ( response => {
30+ if ( ! response . ok ) {
31+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
32+ }
33+ return response . json ( ) ;
34+ } )
35+ . then ( function ( data ) {
36+ map . fitBounds ( [ [ data . ymin , data . xmin ] , [ data . ymax , data . xmax ] ] ) ;
37+ } )
38+ . catch ( error => {
39+ console . error ( 'Fetch error:' , error ) ;
40+ } ) ;
2241
2342 // background layer
2443 const OSMLayer = L . tileLayer ( 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' , {
@@ -34,9 +53,14 @@ function init()
3453 'Authorization' : 'Bearer ' + token . value ,
3554 'Accept' : 'application/json'
3655 } ,
37- method : "GET" } )
38- . then ( response => response . json ( ) )
39- . then ( function ( data ) {
56+ method : "GET" } )
57+ . then ( response => {
58+ if ( ! response . ok ) {
59+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
60+ }
61+ return response . json ( ) ;
62+ } )
63+ . then ( function ( data ) {
4064 const last_date = data . dates [ data . dates . length - 1 ] ;
4165 g_tsdmlayer = new L . TileLayerHeaders ( g_tileurl + '/tsdm/' + last_date + '/' + property_id . value + '/{z}/{x}/{y}' , {
4266 attribution : '© <a href="https://www.cibolabs.com.au/">CiboLabs</a>' ,
@@ -63,11 +87,20 @@ function init()
6387 'Authorization' : 'Bearer ' + token . value ,
6488 'Accept' : 'application/json'
6589 } ,
66- method : "POST" } )
67- . then ( response => response . json ( ) )
68- . then ( function ( data ) {
90+ method : "POST" } )
91+ . then ( response => {
92+ if ( ! response . ok ) {
93+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
94+ }
95+ return response . json ( ) ;
96+ } )
97+ . then ( function ( data ) {
6998 geojsonLayer . addData ( data ) ;
70- } ) ;
99+ } )
100+ . catch ( error => {
101+ console . error ( 'Fetch error:' , error ) ;
102+ } ) ;
103+
71104
72105 // dates - make a slider
73106 let slider = L . control . slider ( function ( value ) {
@@ -101,7 +134,12 @@ function init()
101134 'Accept' : 'application/json'
102135 } ,
103136 method : "POST" } )
104- . then ( response => response . json ( ) )
137+ . then ( response => {
138+ if ( ! response . ok ) {
139+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
140+ }
141+ return response . json ( ) ;
142+ } )
105143 . then ( function ( data ) {
106144
107145 // pull out the data for every paddock
@@ -163,8 +201,13 @@ function init()
163201 }
164202 }
165203 } ) ;
166-
204+ } )
205+ . catch ( error => {
206+ console . error ( 'Fetch error:' , error ) ;
167207 } ) ;
208+ } )
209+ . catch ( error => {
210+ console . error ( 'Fetch error:' , error ) ;
168211 } ) ;
169212 } ) ;
170213 myDialog . showModal ( ) ;
0 commit comments