better geolocation

This commit is contained in:
Swrup 2022-06-27 19:45:58 +02:00
parent f0f7a7b49a
commit 473954be07
2 changed files with 20 additions and 8 deletions

View file

@ -112,6 +112,7 @@ let on_click_set_latlng e =
in
Leaflet.Map.open_popup popup map;
(* TODO add a marker with special icon here *)
let lat = Leaflet.Latlng.lat latlng |> Jstr.of_float in
let lng = Leaflet.Latlng.lng latlng |> Jstr.of_float in
let value_jstr = Jstr.of_string "value" in

View file

@ -76,13 +76,24 @@ module Geolocalize = struct
let geolocalize _ =
log "geolocalize@\n";
let l = Brr_io.Geolocation.of_navigator Brr.G.navigator in
ignore @@ Fut.await (Brr_io.Geolocation.get l) update_location
let () =
let button = find_by_id "geolocalize" in
let (_ : Brr.Ev.listener) =
Brr.Ev.listen Brr.Ev.click geolocalize (Brr.El.as_target button)
let update_location geo =
log "update_location@\n";
match geo with
| Error e ->
(* todo: popup error message for user *)
log "geolocation failure: %s@\n"
@@ Jstr.to_string
@@ Brr_io.Geolocation.Error.message e
| Ok geo ->
(* todo: add a special marker to map *)
let lat = Brr_io.Geolocation.Pos.latitude geo in
let lng = Brr_io.Geolocation.Pos.longitude geo in
let latlng = Leaflet.Latlng.create lat lng in
Leaflet.Map.set_view latlng ~zoom:(Some 17) map
in
()
let l = Brr_io.Geolocation.of_navigator Brr.G.navigator in
let opts = Brr_io.Geolocation.opts ~high_accuracy:true () in
(* todo: use `Geolocation.watch` instead ? it may improve precision *)
ignore @@ Fut.await (Brr_io.Geolocation.get l ~opts) update_location
end