Better feedback reporting; auto pan and zoom now work.

This commit is contained in:
Simon Brooke 2020-02-22 14:32:35 +00:00
parent 98abf86cbe
commit 20507ba5f3
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
7 changed files with 62 additions and 34 deletions

View file

@ -35,7 +35,7 @@ footer a {
h1, h2, h3, h4, h5, h6 { h1, h2, h3, h4, h5, h6 {
width: 100%; width: 100%;
margin: 0; margin: 0;
padding-left: 10%; padding: 0.5em 10%;
text-align: left; text-align: left;
background-color: #3298dc; background-color: #3298dc;
color: white; color: white;
@ -44,3 +44,19 @@ h1, h2, h3, h4, h5, h6 {
samp { samp {
background-color: #b0b0ff; background-color: #b0b0ff;
} }
#error
{
background-color: maroon;
color: white;
margin: 0px;
padding-left: 10%;
}
#message
{
background-color: darkgreen;
color: white;
margin: 0px;
padding-left: 10%;
}

View file

@ -18,9 +18,9 @@ crossorigin=""/ -->
<h4> <h4>
An ultra-lightweight tool to show comma-separated value data on a map An ultra-lightweight tool to show comma-separated value data on a map
</h4> </h4>
<p id="message"></p>
<p id="error"></p>
<div id="app"> <div id="app">
<p id="message"></p>
<p id="error"></p>
<div id="map" style="height: 600px; width: 80%; border: thin solid gray;"></div> <div id="map" style="height: 600px; width: 80%; border: thin solid gray;"></div>
<div id="doc"> <div id="doc">
<p> <p>

View file

@ -4,14 +4,15 @@
[clojure.string :as cs] [clojure.string :as cs]
[geocsv-lite.data :refer [get-csv-url get-data get-query-part-as-map]] [geocsv-lite.data :refer [get-csv-url get-data get-query-part-as-map]]
[geocsv-lite.gis :as gis] [geocsv-lite.gis :as gis]
[geocsv-lite.map :as m])) [geocsv-lite.map :as m]
[geocsv-lite.notify :as n]))
(defn ^:export initialise-map-element (defn ^:export initialise-map-element
"Create a map view in the element with this `id` and decorate it with "Create a map view in the element with this `id` and decorate it with
pins showing locations from this `data-source`." pins showing locations from this `data-source`."
[id data-source] [id data-source]
(js/console.log (str "geocsv-lite.core.initialise-map-element called with args id: " id "; data-source: " data-source ".")) (n/message (str "geocsv-lite.core.initialise-map-element called with args id: " id "; data-source: " data-source "."))
(let [sid (str id) (let [sid (str id)
kid (keyword sid) kid (keyword sid)
v (m/add-view sid 55 -4 10)] v (m/add-view sid 55 -4 10)]

View file

@ -6,8 +6,7 @@
[cljs.core.async :refer [<!]] [cljs.core.async :refer [<!]]
[geocsv-lite.gis :as gis] [geocsv-lite.gis :as gis]
[geocsv-lite.map :refer [get-view]] [geocsv-lite.map :refer [get-view]]
)) [geocsv-lite.notify :as n]))
@ -69,7 +68,7 @@
(rest data)) (rest data))
] ]
(gis/refresh-map-pins (get-view k) records)) (gis/refresh-map-pins (get-view k) records))
(js/console.error (str "Bad response from server: " (:status response))))) (n/error (str "Bad response from server: " (:status response)))))
(defn get-data (defn get-data
@ -90,8 +89,3 @@
(go (let [response (<! (http/get uri))] (go (let [response (<! (http/get uri))]
(apply handler-fn (list response k))))) (apply handler-fn (list response k)))))
;; (go (let [uri "http://localhost:3449/data/data.csv"
;; response (<! (http/get uri))]
;; (when (= (:status response) 200)
;; (default-handler response :map))))

View file

@ -2,7 +2,8 @@
:author "Simon Brooke"} :author "Simon Brooke"}
geocsv-lite.gis geocsv-lite.gis
(:require [cljs.reader :refer [read-string]] (:require [cljs.reader :refer [read-string]]
[clojure.string :as cs])) [clojure.string :as cs]
[geocsv-lite.notify :as n]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -39,28 +40,28 @@
(fn [position] (fn [position]
(let [lat (.-latitude (.-coords position)) (let [lat (.-latitude (.-coords position))
lng (.-longitude (.-coords position))] lng (.-longitude (.-coords position))]
(js/console.log (str "Current location is: " lat ", " lng)) (n/message (str "Current location is: " lat ", " lng))
(if (if
(and view (float? lat) (float? lng)) (and view (float? lat) (float? lng))
(do (do
(.panTo view (.latLng js/L lat lng))) (.panTo view (.latLng js/L lat lng)))
(do (do
(js/console.log (n/message
(if view (if view
(str "Geolocation failed lat: '" lat "'; lng '" lng "'") (str "Geolocation failed lat: '" lat "'; lng '" lng "'")
"No value for subscription to [:view]")) "No value for subscription to [:view]"))
0))))) 0)))))
(do (do
(js/console.log "Geolocation not available") (n/message "Geolocation not available")
0)) 0))
(catch js/Object any (catch js/Object any
(js/console.log "Exception while trying to access location: " + any) (n/error (str "Exception while trying to access location: " any))
0))) 0)))
(defn map-pin-click-handler (defn map-pin-click-handler
[id] [id]
(js/console.log (str "Click handler for record #" id))) (n/message (str "Click handler for record #" id)))
(defn pin-image (defn pin-image
@ -139,7 +140,7 @@
:title (:name record)}))] :title (:name record)}))]
(.bindPopup marker (popup-table-content record)) (.bindPopup marker (popup-table-content record))
(.addTo marker view) (.addTo marker view)
(js/console.log (str "Added `"(:name record)"` in at " lat ", " lng)) (n/message (str "Added `"(:name record)"` in at " lat ", " lng))
marker)))) marker))))
(defn map-remove-pins (defn map-remove-pins
@ -170,10 +171,12 @@
centre of the locations of these records as indicated by the values of their centre of the locations of these records as indicated by the values of their
`:latitude` and `:longitude` keys." `:latitude` and `:longitude` keys."
[records] [records]
(let [lats (map js/Number (map :latitude records)) (let [lats (remove zero?
(filter number? (map js/Number (map :latitude records))))
min-lat (apply min lats) min-lat (apply min lats)
max-lat (apply max lats) max-lat (apply max lats)
lngs (filter js/Number (map :longitude records)) lngs (remove zero?
(filter number? (map js/Number (map :longitude records))))
min-lng (apply min lngs) min-lng (apply min lngs)
max-lng (apply max lngs)] max-lng (apply max lngs)]
(if-not (if-not
@ -189,15 +192,16 @@
[view records] [view records]
(let [view (map-remove-pins view) (let [view (map-remove-pins view)
centre (compute-centre records)] centre (compute-centre records)]
(js/console.log (str "refresh-map-pins called; " (count records) " records, centre at " centre))
(js/console.log (str "Type of longitude " (:longitude (first records)) " is: " (type (:longitude (first records)))))
(if (if
view view
(let [added (remove nil? (map #(add-map-pin %1 %2 view) records (range)))] (do
(js/console.log (str "Adding " (count added) " pins")) (n/message
(str
"Mapped "
(count
(remove nil? (map #(add-map-pin %1 %2 view) records (range))))
" records, centre at " centre))
(if (if
(:latitude centre) (:latitude centre)
(do (.setView view (clj->js [(:latitude centre) (:longitude centre)]) (:zoom centre))))
(js/console.log (str "computed centre: " centre)) (do (n/error "View is not yet ready")))))
(.setView view (clj->js [(:latitude centre) (:longitude centre)]) (:zoom centre)))))
(do (js/console.log "View is not yet ready")))))

View file

@ -1,5 +1,5 @@
(ns geocsv-lite.map (ns geocsv-lite.map
(:require )) (:require [geocsv-lite.notify :as n]))
;;; Cribbed heavily from ;;; Cribbed heavily from
;;; https://github.com/reagent-project/reagent-cookbook/tree/master/recipes/leaflet ;;; https://github.com/reagent-project/reagent-cookbook/tree/master/recipes/leaflet
@ -76,15 +76,15 @@
v (or v (or
(@views k) (@views k)
(map-did-mount id lat lng zoom))] (map-did-mount id lat lng zoom))]
(js/console.log (str "Added Leaflet view to element with id `" id "`")) (n/message (str "Added Leaflet view to element with id `" id "`"))
(swap! views assoc k v) (swap! views assoc k v)
v)) v))
(defn get-view (defn get-view
[k] [k]
(when-not (keyword? k) (js/console.log "Key `" k "` passed to get-view is not a keyword")) (when-not (keyword? k) (n/message (str "Key `" k "` passed to get-view is not a keyword")))
(when-not (k @views) (js/console.log "Key `" k "` does not identify a known view")) (when-not (k @views) (n/message (str "Key `" k "` does not identify a known view")))
(k @views)) (k @views))

View file

@ -0,0 +1,13 @@
(ns geocsv-lite.notify
(:require [clojure.browser.dom :as dom]))
(defn message
[msg]
(js/console.log msg)
(dom/set-text (.getElementById js/document "message") msg))
(defn error
[err]
(js/console.error err)
(dom/set-text (.getElementById js/document "error") err))