leaflet/src/map.ml

59 lines
1.6 KiB
OCaml
Raw Normal View History

2022-04-12 13:46:37 +02:00
(* BSD-2-Clause License *)
2022-04-07 14:45:11 +02:00
type t = Jv.t
2022-04-09 22:21:22 +02:00
let of_jv = Fun.id
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let to_jv = Fun.id
2022-04-07 14:45:11 +02:00
2022-06-18 07:53:26 +02:00
let create_on ?(options = Jv.null) id =
Jv.call Global.leaflet "map" [| Jv.of_string id; options |]
let create_from_div ?(options = Jv.null) container =
if Brr.El.has_tag_name Brr.El.Name.div container then
Jv.call Global.leaflet "map" [| Brr.El.to_jv container; options |]
else failwith "container is not a <div>"
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let invalidate_size map =
let (_ : Jv.t) = Jv.call map "invalidateSize" [| Jv.true' |] in
()
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let fit_world map =
let (_ : Jv.t) = Jv.call map "fitWorld" [||] in
()
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let get_container map = Jv.call (to_jv map) "getContainer" [||] |> Brr.El.of_jv
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let set_view latlng ~zoom map =
let latlng = Latlng.to_jv latlng in
let (_ : Jv.t) =
match zoom with
| None -> Jv.call map "setView" [| latlng |]
| Some zoom -> Jv.call map "setView" [| latlng; Jv.of_int zoom |]
in
()
2022-04-07 14:45:11 +02:00
2022-04-08 22:43:31 +02:00
let on : type kind. kind Event.sub -> (kind Event.t -> 'b) -> t -> unit =
fun event handler map ->
let name = Event.sub_to_string event in
2022-04-09 22:21:22 +02:00
let handler v = handler @@ Event.of_jv event v in
let (_ : Jv.t) = Jv.call map "on" [| Jv.of_string name; Jv.repr handler |] in
()
2022-04-07 14:45:11 +02:00
2022-04-09 22:21:22 +02:00
let get_center map = Latlng.of_jv @@ Jv.call map "getCenter" [||]
2022-04-07 14:45:11 +02:00
let get_zoom map = Jv.call map "getZoom" [||] |> Jv.to_int
let wrap_latlng latlng map =
2022-04-09 22:21:22 +02:00
Latlng.of_jv @@ Jv.call map "wrapLatLng" [| Latlng.to_jv latlng |]
let open_popup popup map =
ignore @@ Jv.call map "openPopup" [| Popup.to_jv popup |]
2022-06-26 05:07:45 +02:00
let close_popup ~popup map =
ignore
@@
match popup with
| None -> Jv.call map "closePopup" [||]
| Some popup -> Jv.call map "closePopup" [| Popup.to_jv popup |]