use leaflet lib
This commit is contained in:
parent
ce7bb9d386
commit
ccba00b2e7
5 changed files with 78 additions and 117 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
version=0.21.0
|
version=0.24.1
|
||||||
assignment-operator=end-line
|
assignment-operator=end-line
|
||||||
break-cases=fit
|
break-cases=fit
|
||||||
break-fun-decl=wrap
|
break-fun-decl=wrap
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ let make_thumbnail content =
|
||||||
Cmd.(
|
Cmd.(
|
||||||
v "convert" % "-define" % "jpeg:size=700x700" % p image_file
|
v "convert" % "-define" % "jpeg:size=700x700" % p image_file
|
||||||
% "-auto-orient" % "-thumbnail" % "300x300>" % "-unsharp" % "0x.5"
|
% "-auto-orient" % "-thumbnail" % "300x300>" % "-unsharp" % "0x.5"
|
||||||
% "-format" % "jpg" % p thumb_file)
|
% "-format" % "jpg" % p thumb_file )
|
||||||
in
|
in
|
||||||
let* () = OS.Cmd.run cmd in
|
let* () = OS.Cmd.run cmd in
|
||||||
let* thumbnail = OS.File.read thumb_file in
|
let* thumbnail = OS.File.read thumb_file in
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
(library
|
(library
|
||||||
(name js_map)
|
(name js_map)
|
||||||
(modules js_map)
|
(modules js_map)
|
||||||
(libraries js_of_ocaml brr)
|
(libraries js_of_ocaml brr leaflet)
|
||||||
(js_of_ocaml
|
(js_of_ocaml
|
||||||
(javascript_files leaflet/leaflet.js))
|
(javascript_files leaflet/leaflet.js))
|
||||||
(preprocess
|
(preprocess
|
||||||
|
|
@ -32,7 +32,7 @@
|
||||||
(executable
|
(executable
|
||||||
(name js_babillard)
|
(name js_babillard)
|
||||||
(modules js_babillard)
|
(modules js_babillard)
|
||||||
(libraries js_of_ocaml brr js_map js_post_form js_pretty_post)
|
(libraries js_of_ocaml brr js_map js_post_form js_pretty_post leaflet)
|
||||||
(modes js)
|
(modes js)
|
||||||
(preprocess
|
(preprocess
|
||||||
(pps js_of_ocaml-ppx)))
|
(pps js_of_ocaml-ppx)))
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ module Visibility = struct
|
||||||
set_visible return_button;
|
set_visible return_button;
|
||||||
set_invisible thread_preview_div;
|
set_invisible thread_preview_div;
|
||||||
Option.iter set_invisible new_thread_button;
|
Option.iter set_invisible new_thread_button;
|
||||||
ignore @@ Jv.call Leaflet.map "closePopup" [||]
|
Leaflet.Map.close_popup ~popup:None map
|
||||||
|
|
||||||
let to_babillard_mode _event =
|
let to_babillard_mode _event =
|
||||||
log "change_page_mode@\n";
|
log "change_page_mode@\n";
|
||||||
|
|
@ -41,7 +41,7 @@ module Visibility = struct
|
||||||
set_invisible return_button;
|
set_invisible return_button;
|
||||||
set_visible thread_preview_div;
|
set_visible thread_preview_div;
|
||||||
Option.iter set_visible new_thread_button;
|
Option.iter set_visible new_thread_button;
|
||||||
ignore @@ Jv.call Leaflet.map "closePopup" [||]
|
Leaflet.Map.close_popup ~popup:None map
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
log "add events on return/new thread button@\n";
|
log "add events on return/new thread button@\n";
|
||||||
|
|
@ -71,15 +71,14 @@ module Marker = struct
|
||||||
log "on_each_feature@\n";
|
log "on_each_feature@\n";
|
||||||
let feature_properties = Jv.get feature "properties" in
|
let feature_properties = Jv.get feature "properties" in
|
||||||
let thread_preview = Jv.get feature_properties "content" in
|
let thread_preview = Jv.get feature_properties "content" in
|
||||||
ignore
|
Leaflet.Layer.on Leaflet.Event.Click (marker_on_click thread_preview) layer
|
||||||
@@ Jv.call layer "on"
|
|
||||||
[| Jv.of_string "click"; Jv.repr (marker_on_click thread_preview) |]
|
|
||||||
|
|
||||||
let handle_geojson geojson =
|
let handle_geojson geojson =
|
||||||
log "handle_geojson@\n";
|
log "handle_geojson@\n";
|
||||||
let dict = Jv.obj [| ("onEachFeature", Jv.repr on_each_feature) |] in
|
let layer =
|
||||||
let layer = Jv.call Leaflet.leaflet "geoJSON" [| geojson; dict |] in
|
Leaflet.Layer.create_geojson geojson [ On_each_feature on_each_feature ]
|
||||||
let _marker_layer = Jv.call layer "addTo" [| Leaflet.map |] in
|
in
|
||||||
|
let _marker_layer = Leaflet.Layer.add_to map layer in
|
||||||
()
|
()
|
||||||
|
|
||||||
let markers_handle_response response =
|
let markers_handle_response response =
|
||||||
|
|
@ -104,22 +103,19 @@ let button = Jv.get Jv.global "submit-new-thread-button"
|
||||||
let on_click_set_latlng e =
|
let on_click_set_latlng e =
|
||||||
log "on_click_set_latlng@\n";
|
log "on_click_set_latlng@\n";
|
||||||
if !Visibility.is_in_new_thread_mode then (
|
if !Visibility.is_in_new_thread_mode then (
|
||||||
let lat_lng = Jv.get e "latlng" in
|
let latlng = Leaflet.Event.latlng e in
|
||||||
ignore @@ Jv.call Leaflet.popup "setLatLng" [| lat_lng |];
|
let popup =
|
||||||
ignore
|
Leaflet.Popup.create ~content:(Some "create thread here")
|
||||||
@@ Jv.call Leaflet.popup "setContent"
|
~latlng:(Some latlng) []
|
||||||
[| Jv.of_string "create thread here" |];
|
in
|
||||||
ignore @@ Jv.call Leaflet.popup "openOn" [| Leaflet.map |];
|
Leaflet.Map.open_popup popup map;
|
||||||
|
|
||||||
let lat = Jv.get lat_lng "lat" in
|
let lat = Leaflet.Latlng.lat latlng |> Jv.of_float in
|
||||||
let lng = Jv.get lat_lng "lng" in
|
let lng = Leaflet.Latlng.lng latlng |> Jv.of_float in
|
||||||
ignore @@ Jv.call lat_input "setAttribute" [| Jv.of_string "value"; lat |];
|
ignore @@ Jv.call lat_input "setAttribute" [| Jv.of_string "value"; lat |];
|
||||||
ignore @@ Jv.call lng_input "setAttribute" [| Jv.of_string "value"; lng |];
|
ignore @@ Jv.call lng_input "setAttribute" [| Jv.of_string "value"; lng |];
|
||||||
|
|
||||||
ignore @@ Jv.call button "removeAttribute" [| Jv.of_string "disabled" |] )
|
ignore @@ Jv.call button "removeAttribute" [| Jv.of_string "disabled" |] )
|
||||||
|
|
||||||
(*add on_click callback to map*)
|
(*add on_click callback to map*)
|
||||||
let () =
|
let () = Leaflet.Map.on Leaflet.Event.Click on_click_set_latlng map
|
||||||
ignore
|
|
||||||
@@ Jv.call Leaflet.map "on"
|
|
||||||
[| Jv.of_string "click"; Jv.repr on_click_set_latlng |]
|
|
||||||
|
|
|
||||||
107
src/js/js_map.ml
107
src/js/js_map.ml
|
|
@ -1,101 +1,68 @@
|
||||||
let log = Format.printf
|
let log = Format.printf
|
||||||
|
|
||||||
module Leaflet = struct
|
let map = Leaflet.Map.create_on "map"
|
||||||
(* get the leaflet object *)
|
|
||||||
let leaflet =
|
|
||||||
match Jv.(find global "L") with
|
|
||||||
| Some l -> l
|
|
||||||
| None -> failwith "can't load leaflet"
|
|
||||||
|
|
||||||
(* get popup object *)
|
let () =
|
||||||
let popup = Jv.call leaflet "popup" [||]
|
let osm_layer = Leaflet.Layer.create_tile_osm None in
|
||||||
|
Leaflet.Layer.add_to map osm_layer
|
||||||
|
|
||||||
(* create a map *)
|
let storage = Brr_io.Storage.local Brr.G.window
|
||||||
let map =
|
|
||||||
log "creating map@\n";
|
|
||||||
let open Brr in
|
|
||||||
let _container = El.div ~at:At.[ id (Jstr.v "map") ] [] in
|
|
||||||
Jv.call leaflet "map" [| Jv.of_string "map" |]
|
|
||||||
|
|
||||||
(* create map tile layer *)
|
(* set map's view *)
|
||||||
let tile_layer =
|
(* try to set map's view to last position viewed by using web storage *)
|
||||||
log "creating tile layer@\n";
|
let () =
|
||||||
Jv.call leaflet "tileLayer"
|
|
||||||
[| Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
||||||
; Jv.obj
|
|
||||||
[| ( "attribution"
|
|
||||||
, Jv.of_string
|
|
||||||
{|© <a href="https://.www.openstreetmap.org/copyright">OpenStreetMap</a> contributors|}
|
|
||||||
)
|
|
||||||
|]
|
|
||||||
|]
|
|
||||||
|
|
||||||
(* add tile layer *)
|
|
||||||
let () =
|
|
||||||
log "adding tile layer@\n";
|
|
||||||
let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in
|
|
||||||
()
|
|
||||||
|
|
||||||
let storage = Brr_io.Storage.local Brr.G.window
|
|
||||||
|
|
||||||
(* set map's view *)
|
|
||||||
(* try to set map's view to last position viewed by using web storage *)
|
|
||||||
let () =
|
|
||||||
log "setting view@\n";
|
log "setting view@\n";
|
||||||
let lat = Brr_io.Storage.get_item storage (Jstr.of_string "lat") in
|
let lat = Brr_io.Storage.get_item storage (Jstr.of_string "lat") in
|
||||||
let lng = Brr_io.Storage.get_item storage (Jstr.of_string "lng") in
|
let lng = Brr_io.Storage.get_item storage (Jstr.of_string "lng") in
|
||||||
let zoom = Brr_io.Storage.get_item storage (Jstr.of_string "zoom") in
|
let zoom = Brr_io.Storage.get_item storage (Jstr.of_string "zoom") in
|
||||||
match (lat, lng, zoom) with
|
match (lat, lng, zoom) with
|
||||||
| Some lat, Some lng, Some zoom ->
|
| Some lat, Some lng, Some zoom ->
|
||||||
let latlng =
|
let lat = Jstr.to_float lat in
|
||||||
Jv.call leaflet "latLng" [| Jv.of_jstr lat; Jv.of_jstr lng |]
|
let lng = Jstr.to_float lng in
|
||||||
|
let zoom =
|
||||||
|
match Jstr.to_int zoom with
|
||||||
|
| None -> failwith "view storage bug"
|
||||||
|
| Some zoom -> Some zoom
|
||||||
in
|
in
|
||||||
ignore @@ Jv.call map "setView" [| latlng; Jv.of_jstr zoom |]
|
let latlng = Leaflet.Latlng.create lat lng in
|
||||||
|
ignore @@ Leaflet.Map.set_view latlng ~zoom map
|
||||||
| _ ->
|
| _ ->
|
||||||
let latlng =
|
let latlng = Leaflet.Latlng.create 51.505 (-0.09) in
|
||||||
Jv.call leaflet "latLng" [| Jv.of_float 51.505; Jv.of_float (-0.09) |]
|
ignore @@ Leaflet.Map.set_view latlng ~zoom:(Some 13) map
|
||||||
in
|
|
||||||
ignore @@ Jv.call map "setView" [| latlng; Jv.of_int 13 |]
|
|
||||||
|
|
||||||
let on_moveend _event =
|
let on_moveend _event =
|
||||||
log "on moveend event@\n";
|
log "on moveend event@\n";
|
||||||
let latlng = Jv.call map "getCenter" [||] in
|
let latlng = Leaflet.Map.get_center map in
|
||||||
(*we need to wrap coordinates so we don't drift into a parralel universe and lose track of markers :^) *)
|
(*we need to wrap coordinates so we don't drift into a parralel universe and lose track of markers :^) *)
|
||||||
let wrapped_latlng = Jv.call map "wrapLatLng" [| latlng |] in
|
let wrapped_latlng = Leaflet.Map.wrap_latlng latlng map in
|
||||||
let lat = Jv.get wrapped_latlng "lat" in
|
let lat = Leaflet.Latlng.lat latlng |> Jv.of_float |> Jv.to_jstr in
|
||||||
let lng = Jv.get wrapped_latlng "lng" in
|
let lng = Leaflet.Latlng.lng latlng |> Jv.of_float |> Jv.to_jstr in
|
||||||
match
|
match Brr_io.Storage.set_item storage (Jstr.of_string "lat") lat with
|
||||||
Brr_io.Storage.set_item storage (Jstr.of_string "lat") (Jv.to_jstr lat)
|
|
||||||
with
|
|
||||||
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
||||||
| Ok () -> (
|
| Ok () -> (
|
||||||
match
|
match Brr_io.Storage.set_item storage (Jstr.of_string "lng") lng with
|
||||||
Brr_io.Storage.set_item storage (Jstr.of_string "lng") (Jv.to_jstr lng)
|
|
||||||
with
|
|
||||||
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
||||||
| Ok () ->
|
| Ok () ->
|
||||||
let is_wrapped =
|
let is_wrapped = not @@ Leaflet.Latlng.equals latlng wrapped_latlng in
|
||||||
not @@ Jv.to_bool @@ Jv.call latlng "equals" [| wrapped_latlng |]
|
|
||||||
in
|
|
||||||
if is_wrapped then (
|
if is_wrapped then (
|
||||||
log "setView to wrapped coordinate@\n";
|
log "setView to wrapped coordinate@\n";
|
||||||
(* warning: calling setView in on_moveend can cause recursion *)
|
(* warning: calling setView in on_moveend can cause recursion *)
|
||||||
ignore @@ Jv.call map "setView" [| wrapped_latlng |] ) )
|
Leaflet.Map.set_view wrapped_latlng ~zoom:None map ) )
|
||||||
|
|
||||||
let on_zoomend _event =
|
let on_zoomend _event =
|
||||||
log "on zoomend event@\n";
|
log "on zoomend event@\n";
|
||||||
let zoom = Jv.call map "getZoom" [||] in
|
let zoom = Leaflet.Map.get_zoom map in
|
||||||
match
|
match
|
||||||
Brr_io.Storage.set_item storage (Jstr.of_string "zoom") (Jv.to_jstr zoom)
|
Brr_io.Storage.set_item storage (Jstr.of_string "zoom")
|
||||||
|
(Jv.to_jstr @@ Jv.of_int zoom)
|
||||||
with
|
with
|
||||||
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
| (exception Jv.Error _) | Error _ -> failwith "can't set latlng storage"
|
||||||
| Ok () -> ()
|
| Ok () -> ()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
log "add on (move/zoom)end event@\n";
|
log "add on (move/zoom)end event@\n";
|
||||||
ignore @@ Jv.call map "on" [| Jv.of_string "moveend"; Jv.repr on_moveend |];
|
Leaflet.Map.on Leaflet.Event.Move_end on_moveend map;
|
||||||
ignore @@ Jv.call map "on" [| Jv.of_string "zoomend"; Jv.repr on_zoomend |]
|
Leaflet.Map.on Leaflet.Event.Zoom_end on_zoomend map
|
||||||
end
|
|
||||||
|
|
||||||
module Geolocalize = struct
|
module Geolocalize = struct
|
||||||
let update_location geo =
|
let update_location geo =
|
||||||
|
|
@ -105,10 +72,8 @@ module Geolocalize = struct
|
||||||
| Ok geo ->
|
| Ok geo ->
|
||||||
let lat = Brr_io.Geolocation.Pos.latitude geo in
|
let lat = Brr_io.Geolocation.Pos.latitude geo in
|
||||||
let lng = Brr_io.Geolocation.Pos.longitude geo in
|
let lng = Brr_io.Geolocation.Pos.longitude geo in
|
||||||
let latlng =
|
let latlng = Leaflet.Latlng.create lat lng in
|
||||||
Jv.call Leaflet.leaflet "latLng" [| Jv.of_float lat; Jv.of_float lng |]
|
Leaflet.Map.set_view latlng ~zoom:(Some 13) map
|
||||||
in
|
|
||||||
ignore @@ Jv.call Leaflet.map "setView" [| latlng; Jv.of_int 13 |]
|
|
||||||
|
|
||||||
let geolocalize _ =
|
let geolocalize _ =
|
||||||
log "geolocalize@\n";
|
log "geolocalize@\n";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue