2022-04-12 13:46:37 +02:00
|
|
|
(* BSD-2-Clause License *)
|
|
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
type _ t =
|
|
|
|
|
| Basic : Jv.t -> [> `Basic ] t
|
|
|
|
|
| Geojson : Jv.t -> [> `Geojson ] t
|
|
|
|
|
| Marker : Jv.t -> [> `Marker ] t
|
|
|
|
|
| Tile : Jv.t -> [> `Tile ] t
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-06-20 06:52:05 +02:00
|
|
|
type _ sub =
|
|
|
|
|
| Basic : [> `Basic ] sub
|
|
|
|
|
| Geojson : [> `Geojson ] sub
|
|
|
|
|
| Marker : [> `Marker ] sub
|
|
|
|
|
| Tile : [> `Tile ] sub
|
|
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
(** Basic layers *)
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let add_to : type kind. Map.t -> kind t -> unit =
|
|
|
|
|
fun map -> function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "addTo" [| Map.to_jv map |] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let remove : type kind. kind t -> unit = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "remove" [||] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let remove_from : type kind. Map.t -> kind t -> unit =
|
|
|
|
|
fun map -> function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "removeFrom" [| Map.to_jv map |] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-06-20 06:52:05 +02:00
|
|
|
let bind_popup : type kind. Brr.El.t -> ?options:Jv.t -> kind t -> unit =
|
|
|
|
|
fun el ?(options = Jv.null) -> function
|
2022-04-09 22:21:22 +02:00
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
2022-06-20 06:52:05 +02:00
|
|
|
let (_ : Jv.t) = Jv.call l "bindPopup" [| Brr.El.to_jv el; options |] in
|
2022-04-09 22:21:22 +02:00
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let unbind_popup : type kind. kind t -> unit = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "unbindPopup" [||] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let open_popup : type kind. kind t -> unit = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "openPopup" [||] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let close_popup : type kind. kind t -> unit = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
let (_ : Jv.t) = Jv.call l "closePopup" [||] in
|
|
|
|
|
()
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let get_popup : type kind. kind t -> Popup.t = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l ->
|
|
|
|
|
Jv.call l "getPopup" [||] |> Popup.of_jv
|
2022-04-08 13:21:20 +02:00
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
let to_jv : type kind. kind t -> Jv.t = function
|
|
|
|
|
| Basic l | Geojson l | Marker l | Tile l -> l
|
|
|
|
|
|
2022-06-20 06:52:05 +02:00
|
|
|
let of_jv : type kind. kind sub -> Jv.t -> kind t =
|
|
|
|
|
fun tag l ->
|
|
|
|
|
match tag with
|
|
|
|
|
| Basic -> Basic l
|
|
|
|
|
| Geojson -> Geojson l
|
|
|
|
|
| Marker -> Marker l
|
|
|
|
|
| Tile -> Tile l
|
|
|
|
|
|
|
|
|
|
let on : type kind. kind Event.sub -> (kind Event.t -> 'b) -> 'c t -> unit =
|
|
|
|
|
fun event handler layer ->
|
|
|
|
|
let name = Event.sub_to_string event in
|
|
|
|
|
let handler v = handler @@ Event.of_jv event v in
|
|
|
|
|
let (_ : Jv.t) =
|
|
|
|
|
Jv.call (to_jv layer) "on" [| Jv.of_string name; Jv.repr handler |]
|
|
|
|
|
in
|
|
|
|
|
()
|
|
|
|
|
|
2022-04-09 22:21:22 +02:00
|
|
|
(** Geojson layers *)
|
|
|
|
|
|
|
|
|
|
let create_geojson : ?options:Jv.t -> Jv.t -> [ `Geojson ] t =
|
|
|
|
|
fun ?(options = Jv.null) geojson ->
|
|
|
|
|
let jv_t = Jv.call Global.leaflet "geoJSON" [| geojson; options |] in
|
|
|
|
|
Geojson jv_t
|
|
|
|
|
|
|
|
|
|
(** Marker layers *)
|
|
|
|
|
|
2022-06-20 08:16:21 +02:00
|
|
|
type marker_opt =
|
|
|
|
|
| Icon of Icon.t
|
|
|
|
|
| Keyboard of bool
|
|
|
|
|
| Title of string
|
|
|
|
|
| Alt of string
|
|
|
|
|
| Z_index_offset of int
|
|
|
|
|
| Opacity of float
|
|
|
|
|
| Rise_on_hover of bool
|
|
|
|
|
| Rise_offset of int
|
|
|
|
|
| Pane of string
|
|
|
|
|
| Shadow_pane of string
|
|
|
|
|
| Bubbling_mouse_events of bool
|
|
|
|
|
| Auto_pan_on_focus of bool
|
|
|
|
|
|
|
|
|
|
let marker_opt_to_string : marker_opt -> string = function
|
|
|
|
|
| Icon _ -> "icon"
|
|
|
|
|
| Keyboard _ -> "keyboard"
|
|
|
|
|
| Title _ -> "title"
|
|
|
|
|
| Alt _ -> "alt"
|
|
|
|
|
| Z_index_offset _ -> "zIndexOffset"
|
|
|
|
|
| Opacity _ -> "opacity"
|
|
|
|
|
| Rise_on_hover _ -> "riseOnHover"
|
|
|
|
|
| Rise_offset _ -> "riseOffset"
|
|
|
|
|
| Pane _ -> "pane"
|
|
|
|
|
| Shadow_pane _ -> "shadowPane"
|
|
|
|
|
| Bubbling_mouse_events _ -> "bubblingMouseEvents"
|
|
|
|
|
| Auto_pan_on_focus _ -> "autoPanOnFocus"
|
|
|
|
|
|
|
|
|
|
let marker_opt_to_jv = function
|
|
|
|
|
| Icon icon -> Icon.to_jv icon
|
|
|
|
|
| Keyboard b | Rise_on_hover b | Bubbling_mouse_events b | Auto_pan_on_focus b
|
|
|
|
|
->
|
|
|
|
|
Jv.of_bool b
|
|
|
|
|
| Title s | Alt s | Pane s | Shadow_pane s -> Jv.of_string s
|
|
|
|
|
| Z_index_offset i | Rise_offset i -> Jv.of_int i
|
|
|
|
|
| Opacity f -> Jv.of_float f
|
|
|
|
|
|
|
|
|
|
let create_marker : Latlng.t -> marker_opt list -> [ `Marker ] t =
|
|
|
|
|
fun latlng options ->
|
|
|
|
|
let l =
|
|
|
|
|
Array.of_list
|
|
|
|
|
@@ List.map (fun o -> (marker_opt_to_string o, marker_opt_to_jv o)) options
|
|
|
|
|
in
|
|
|
|
|
let jv_t =
|
|
|
|
|
Jv.call Global.leaflet "marker" [| Latlng.to_jv latlng; Jv.obj l |]
|
|
|
|
|
in
|
2022-04-09 22:21:22 +02:00
|
|
|
Marker jv_t
|
|
|
|
|
|
|
|
|
|
(** Tile layers *)
|
|
|
|
|
|
|
|
|
|
let create_tile_osm : string option -> [ `Tile ] t =
|
|
|
|
|
fun url ->
|
|
|
|
|
let url =
|
|
|
|
|
Option.value url
|
|
|
|
|
~default:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
|
|
|
in
|
|
|
|
|
let jv_t =
|
|
|
|
|
Jv.call Global.leaflet "tileLayer"
|
|
|
|
|
[| Jv.of_string url
|
|
|
|
|
; Jv.obj
|
|
|
|
|
[| ( "attribution"
|
|
|
|
|
, Jv.of_string
|
|
|
|
|
"© <a \
|
|
|
|
|
href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \
|
|
|
|
|
contributors" )
|
|
|
|
|
|]
|
|
|
|
|
|]
|
|
|
|
|
in
|
|
|
|
|
Tile jv_t
|