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 in
Leaflet.Map.open_popup popup map; 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 lat = Leaflet.Latlng.lat latlng |> Jstr.of_float in
let lng = Leaflet.Latlng.lng latlng |> Jstr.of_float in let lng = Leaflet.Latlng.lng latlng |> Jstr.of_float in
let value_jstr = Jstr.of_string "value" in let value_jstr = Jstr.of_string "value" in

View file

@ -76,13 +76,24 @@ module Geolocalize = struct
let geolocalize _ = let geolocalize _ =
log "geolocalize@\n"; log "geolocalize@\n";
let l = Brr_io.Geolocation.of_navigator Brr.G.navigator in let update_location geo =
ignore @@ Fut.await (Brr_io.Geolocation.get l) update_location log "update_location@\n";
match geo with
let () = | Error e ->
let button = find_by_id "geolocalize" in (* todo: popup error message for user *)
let (_ : Brr.Ev.listener) = log "geolocation failure: %s@\n"
Brr.Ev.listen Brr.Ev.click geolocalize (Brr.El.as_target button) @@ 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 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 end