@@ -11,8 +11,8 @@ import Prelude
1111-- Data imports
1212import Data.Array ((!!), catMaybes , length )
1313import Data.Maybe (Maybe (..), fromMaybe , isJust , maybe' )
14- import Data.Foldable (sequence_ , for_ )
15- import Data.Traversable (sequence , traverse )
14+ import Data.Foldable (sequence_ )
15+ import Data.Traversable (sequence )
1616import Data.Tuple (Tuple (..), fst , snd )
1717import Math (pi )
1818
@@ -46,7 +46,6 @@ import Web.DOM.ParentNode as WDPN
4646
4747-- Openlayers imports
4848import OpenLayers.Interaction.Select as Select
49- import OpenLayers.MapBrowserEvent as MapBrowserEvent
5049import OpenLayers.Feature as Feature
5150import OpenLayers.Source.OSM as OSM
5251import OpenLayers.Layer.Tile as Tile
@@ -68,12 +67,13 @@ import OpenLayers.Collection as Collection
6867import OpenLayers.Events.Condition as Condition
6968import OpenLayers.Coordinate as Coordinate
7069import OpenLayers.Style.RegularShape as RegularShape
70+ import OpenLayers.Size as Size
71+ import OpenLayers.Render.Event as Event
7172
7273-- Our own imports
73- import Accessibility.Data.Route (Page (..)) as ADR
7474import Accessibility.Component.HTML.Utils (css )
7575import Accessibility.Util.Result (evaluateResult )
76- import Accessibility.Interface.Navigate (class ManageNavigation , gotoPage )
76+ import Accessibility.Interface.Navigate (class ManageNavigation )
7777import Accessibility.Interface.Item (class ManageItem , queryItems , Item )
7878import Accessibility.Interface.Entity (class ManageEntity , Value , queryEntities , Entity (..))
7979
@@ -107,18 +107,13 @@ initialState _ = { subscription : []
107107data Action = Initialize
108108 | Finalize
109109 | Update
110- | UpdateCursor
111- | EditItem
112110 | Center
113- | CenterCursor
114- | AddItem
115- | AddItemCursor
116111 | FeatureSelect Select.SelectEvent
117112 | GPSError
118113 | GPSPosition Geolocation.Geolocation Feature.Feature
119114 | GPSAccuracy Geolocation.Geolocation Feature.Feature
120115 | GPSCenter Geolocation.Geolocation Map.Map VectorLayer.Vector
121- | MAPPosition Feature.Feature MapBrowserEvent.MapBrowserEvent
116+ | MAPRenderComplete Event.RenderEvent
122117
123118-- | The output from this component
124119data Output = AuthenticationError
@@ -147,7 +142,7 @@ render :: forall m . MonadAff m
147142 -> H.ComponentHTML Action () m -- ^ The components HTML
148143render state = HH .div
149144 [css " d-flex flex-column ha-nearby" ]
150- [HH .div [css " row" ] [HH .div[css " col-xs-12 col-md-12" ][HH .h2 [] [HH .text " POI Administration " ]]] ,
145+ [HH .div [css " row" ] [HH .div[css " col-xs-12 col-md-12" ][HH .h2 [] [HH .text " Points Of Interest " ]]] ,
151146 HH .div [css " row flex-grow-1 ha-nearby-map" ] [HH .div[css " col-xs-12 col-md-12" ][HH .div [HP .id_ " ha-map" ][]]]
152147 ]
153148
@@ -183,14 +178,6 @@ handleAction Initialize = do
183178 , geo = Just gps
184179 , layer = Just poiLayer})
185180
186- -- | Add an item to the database based on the current position
187- handleAction AddItem = do
188- state <- H .get
189- mpos <- H .liftEffect $ join <$> traverse Geolocation .getPosition state.geo
190- for_ mpos \pos -> do
191- sequence_ $ gotoPage <$> (ADR.AddPoint <$> (Coordinate .latitude $ Proj .toLonLat' pos)
192- <*> (Coordinate .longitude $ Proj .toLonLat' pos))
193-
194181-- | Finalize action, clean up the component
195182handleAction Finalize = do
196183 state <- H .get
@@ -200,61 +187,35 @@ handleAction Finalize = do
200187 sequence_ $ Map .clearTarget <$> state.map
201188 H .put state { map = Nothing , geo = Nothing , select = Nothing }
202189
203- -- | Add an item to the database based on the current position
204- handleAction AddItemCursor = do
205- state <- H .get
206- for_ state.crosshair \pos -> do
207- sequence_ $ gotoPage <$> (ADR.AddPoint <$> (Coordinate .latitude $ Proj .toLonLat' pos)
208- <*> (Coordinate .longitude $ Proj .toLonLat' pos))
209-
210- -- |Edit the selected item
211- handleAction EditItem = do
212- state <- H .get
213- H .liftEffect $ log " Edit a selected item"
214- cf <- H .liftEffect $ sequence $ Select .getFeatures <$> state.select
215- sequence_ $ gotoPage <$> (ADR.Point <$> (feature cf) <*> (Just false ))
216- where
217- feature cf = join $ (Feature .get " id" ) <$> (join $ (Collection .item 0 ) <$> cf)
218-
219190-- | Update the POI around the GPS location
220191handleAction Update = do
221192
222193 -- Clear the alert
223194 state <- H .modify $ _ {alert = Nothing }
224195
225- -- Get the GPS position
226- pos <- H .liftEffect $ join <$> (sequence $ Geolocation .getPosition <$> state.geo)
227-
228- -- Get the POI from our own backend
229- when (isJust pos) do
230-
231- ditems <- queryItems {longitude : join $ (Coordinate .longitude <<< Proj .toLonLat') <$> pos
232- , latitude: join $ (Coordinate .latitude <<< Proj .toLonLat') <$> pos
233- , distance: Just state.distance
234- , limit: Nothing
235- , text: Nothing } >>= evaluateResult AuthenticationError
236- vs <- H .liftEffect $ maybe' (\_-> VectorSource .create') (\i-> do
237- flist <- sequence $ fromItem <$> i
238- VectorSource .create { features: VectorSource .features.asArray flist }) ditems
239-
240- -- Set the source to the POI-layer
241- H .liftEffect $ sequence_ $ (VectorLayer .setSource vs) <$> state.layer
196+ -- Get the MAP center position
197+ pos <- H .liftEffect do
198+ view <- join <$> (sequence $ Map .getView <$> state.map)
199+ join <$> (sequence $ View .getCenter <$> view)
242200
243- -- Set the alert
244- (Alert <$> H .gets _.alert) >>= H .raise
201+ -- Get the size of the view in meters
202+ distance <- H .liftEffect do
203+ view <- join <$> (sequence $ Map .getView <$> state.map)
204+ resolution <- join <$> (sequence $ View .getResolution <$> view)
205+ size <- join <$> (sequence $ Map .getSize <$> state.map)
206+ pure $ div <$> (max <$> (mul <$> resolution <*> (join (Size .width <$> size)))
207+ <*> (mul <$> resolution <*> (join (Size .height <$> size))))
208+ <*> Just 2.0
245209
246- -- | Update the POI around the current cursor/crosshair
247- handleAction UpdateCursor = do
210+ H .liftEffect $ log $ " Radius for search is " <> (show distance)
211+ H .liftEffect $ log $ " Position for search is " <> (show pos)
248212
249- -- Clear the alert
250- state <- H .modify $ _ {alert = Nothing }
251-
252213 -- Get the POI from our own backend
253- when (isJust state.crosshair ) do
214+ when (isJust pos ) do
254215
255- ditems <- queryItems {longitude : join $ (Coordinate .longitude <<< Proj .toLonLat') <$> state.crosshair
256- , latitude: join $ (Coordinate .latitude <<< Proj .toLonLat') <$> state.crosshair
257- , distance: Just state.distance
216+ ditems <- queryItems {longitude : join $ (Coordinate .longitude <<< Proj .toLonLat') <$> pos
217+ , latitude: join $ (Coordinate .latitude <<< Proj .toLonLat') <$> pos
218+ , distance: Just $ fromMaybe state.distance distance
258219 , limit: Nothing
259220 , text: Nothing } >>= evaluateResult AuthenticationError
260221 vs <- H .liftEffect $ maybe' (\_-> VectorSource .create') (\i-> do
@@ -275,13 +236,6 @@ handleAction Center = do
275236 view <- join <$> (sequence $ Map .getView <$> state.map)
276237 sequence_ $ View .setCenter <$> pos <*> view
277238
278- -- | Center the map around the Cursor/Crosshair
279- handleAction CenterCursor = do
280- state <- H .get
281- H .liftEffect do
282- view <- join <$> (sequence $ Map .getView <$> state.map)
283- sequence_ $ View .setCenter <$> state.crosshair <*> view
284-
285239-- | Feature is selected
286240handleAction (FeatureSelect e) = H .liftEffect $ do
287241 log " Feature selected!"
@@ -305,17 +259,27 @@ handleAction (GPSAccuracy geo feature) = H.liftEffect $ do
305259-- | GPS Center - Center the map based on geolocation and add all POI:s
306260handleAction (GPSCenter geo map vl) = do
307261 state <- H .get
262+
308263 pos <- H .liftEffect $ Geolocation .getPosition geo
309264 H .liftEffect $ do
310265 mv <- Map .getView map
311266 sequence_ $ View .setCenter <$> pos <*> mv
312267
268+ -- Get the size of the view in meters
269+ distance <- H .liftEffect do
270+ view <- Map .getView map
271+ resolution <- join <$> (sequence $ View .getResolution <$> view)
272+ size <- join <$> (sequence $ Map .getSize <$> state.map)
273+ pure $ div <$> (max <$> (mul <$> resolution <*> (join (Size .width <$> size)))
274+ <*> (mul <$> resolution <*> (join (Size .height <$> size))))
275+ <*> Just 2.0
276+
313277 -- Get the POI from our own backend
314278 when (isJust pos) do
315279
316280 ditems <- queryItems {longitude : join $ (Coordinate .longitude <<< Proj .toLonLat') <$> pos
317281 , latitude: join $ (Coordinate .latitude <<< Proj .toLonLat') <$> pos
318- , distance: Just state.distance
282+ , distance: Just $ fromMaybe state.distance distance
319283 , limit: Nothing
320284 , text: Nothing } >>= evaluateResult AuthenticationError
321285 vs <- H .liftEffect $ maybe' (\_-> VectorSource .create') (\i-> do
@@ -329,11 +293,8 @@ handleAction (GPSCenter geo map vl) = do
329293 (Alert <$> H .gets _.alert) >>= H .raise
330294
331295-- | Position the cursor/croasshair on the MAP
332- handleAction (MAPPosition f mbe) = do
333- H .modify_ $ _ {crosshair = Just $ MapBrowserEvent .coordinate mbe}
334- H .liftEffect $ do
335- point <- Point .create' $ MapBrowserEvent .coordinate mbe
336- Feature .setGeometry point f
296+ handleAction (MAPRenderComplete e) = do
297+ H .liftEffect $ log $ " Render completed!"
337298
338299--
339300-- Creates the map and attaches openstreetmap as a source
@@ -355,41 +316,21 @@ createMap = do
355316
356317 -- Extend the map with a set of buttons
357318 ctrl <- Ctrl .defaults'
358- elemAdd <- createMapButton " A" " ha-id-add-item" " ha-map-add-item"
359- elemSAdd <- createMapButton " a" " ha-id-sadd-item" " ha-map-sadd-item"
360- elemEdit <- createMapButton " E" " ha-id-edit-item" " ha-map-edit-item"
361319 elemCenter <- createMapButton " C" " ha-id-center" " ha-map-center"
362- elemSCenter <- createMapButton " c" " ha-id-scenter" " ha-map-scenter"
363320 elemRefresh <- createMapButton " R" " ha-id-refresh" " ha-map-refresh"
364- elemSRefresh <- createMapButton " r" " ha-id-srefresh" " ha-map-srefresh"
365321
366322 domDocument <- window >>= WHW .document <#> WHHD .toDocument
367323 elem <- WDD .createElement " div" domDocument
368324 WDE .setClassName " ha-map-ctrl ol-unselectable ol-control" elem
369325
370326 elem1 <- WDD .createElement " div" domDocument
371- WDE .setClassName " tomas" elem1
372- void $ WDN .appendChild (WDE .toNode elemAdd) (WDE .toNode elem1)
373- void $ WDN .appendChild (WDE .toNode elemSAdd) (WDE .toNode elem1)
327+ void $ WDN .appendChild (WDE .toNode elemRefresh) (WDE .toNode elem1)
374328 void $ WDN .appendChild (WDE .toNode elem1) (WDE .toNode elem)
375329
376330 elem2 <- WDD .createElement " div" domDocument
377- WDE .setClassName " tomas" elem2
378- void $ WDN .appendChild (WDE .toNode elemEdit) (WDE .toNode elem2)
331+ void $ WDN .appendChild (WDE .toNode elemCenter) (WDE .toNode elem2)
379332 void $ WDN .appendChild (WDE .toNode elem2) (WDE .toNode elem)
380333
381- elem3 <- WDD .createElement " div" domDocument
382- WDE .setClassName " tomas" elem3
383- void $ WDN .appendChild (WDE .toNode elemRefresh) (WDE .toNode elem3)
384- void $ WDN .appendChild (WDE .toNode elemSRefresh) (WDE .toNode elem3)
385- void $ WDN .appendChild (WDE .toNode elem3) (WDE .toNode elem)
386-
387- elem4 <- WDD .createElement " div" domDocument
388- WDE .setClassName " tomas" elem4
389- void $ WDN .appendChild (WDE .toNode elemCenter) (WDE .toNode elem4)
390- void $ WDN .appendChild (WDE .toNode elemSCenter) (WDE .toNode elem4)
391- void $ WDN .appendChild (WDE .toNode elem4) (WDE .toNode elem)
392-
393334 ctrlButtons <- Control .create { element: elem }
394335
395336 -- Create the map and set up the controls, layers and view
@@ -430,14 +371,9 @@ createButtonHandlers:: forall o m . MonadAff m
430371createButtonHandlers = do
431372
432373 -- Add a listener to every button on the map
433- sadd <- addMapButtonHandler AddItem " #ha-id-add-item"
434- ssadd <- addMapButtonHandler AddItemCursor " #ha-id-sadd-item"
435- sedit <- addMapButtonHandler EditItem " #ha-id-edit-item"
436374 supd <- addMapButtonHandler Update " #ha-id-refresh"
437- ssupd <- addMapButtonHandler UpdateCursor " #ha-id-srefresh"
438375 scen <- addMapButtonHandler Center " #ha-id-center"
439- sscen <- addMapButtonHandler CenterCursor " #ha-id-scenter"
440- pure $ catMaybes [sadd, sedit, supd, scen, ssadd, ssupd, sscen]
376+ pure $ catMaybes [supd, scen]
441377
442378 where
443379
@@ -578,12 +514,12 @@ createLayers map = do
578514 Map .addLayer plvector map
579515 pure pfeat
580516
581- -- Get a MapBrowser Event for singleclick
517+ -- Get a RenderComplete Event when the rendering is complete
582518 void $ H .subscribe $ HQE .effectEventSource \emitter -> do
583- key <- Map .on " singleclick " (\e -> do
584- HQE .emit emitter (MAPPosition fcursor e)
519+ key <- Map .onRenderComplete (\e -> do
520+ HQE .emit emitter (MAPRenderComplete e)
585521 pure true ) map
586- pure (HQE.Finalizer (Map .un " singleclick " key map))
522+ pure (HQE.Finalizer (Map .unRenderComplete key map))
587523
588524 -- Get the weather data from the IoT Hub
589525 dentities <- queryEntities " WeatherObserved" >>= evaluateResult AuthenticationError
0 commit comments