add more functions

This commit is contained in:
Swrup 2022-04-06 19:56:13 +02:00
parent 2fe7c1450d
commit 2154a10db4
2 changed files with 96 additions and 22 deletions

View file

@ -17,7 +17,10 @@ module LatLng = struct
Jv.call leaflet "latLng" [| Jv.of_float lat; Jv.of_float lng |] Jv.call leaflet "latLng" [| Jv.of_float lat; Jv.of_float lng |]
let lat latlng = Jv.get latlng "lat" |> Jv.to_float let lat latlng = Jv.get latlng "lat" |> Jv.to_float
let lng latlng = Jv.get latlng "lng" |> Jv.to_float let lng latlng = Jv.get latlng "lng" |> Jv.to_float
let equals a b = Jv.call a "equals" [| b |] |> Jv.to_bool
end end
module Ev = struct module Ev = struct
@ -31,21 +34,36 @@ end
module Map = struct module Map = struct
type t = Jv.t type t = Jv.t
let create ?(options = Jv.null) el = let create ?(options = Jv.null) container_id =
Jv.call leaflet "map" [| El.to_jv el; options |] Jv.call leaflet "map" [| Jv.of_string container_id; options |]
let invalidate_size map = let invalidate_size map =
ignore @@ Jv.call map "invalidateSize" [| Jv.true' |] ignore @@ Jv.call map "invalidateSize" [| Jv.true' |]
let fit_world map = ignore @@ Jv.call map "fitWorld" [||] let fit_world map = ignore @@ Jv.call map "fitWorld" [||]
let get_container map = Jv.call map "getContainer" [||] |> El.of_jv let get_container map = Jv.call map "getContainer" [||] |> El.of_jv
let set_view latlng ~zoom map = let set_view latlng ?zoom map =
ignore @@ Jv.call map "setView" [| latlng; Jv.of_int zoom |]; ignore
map @@
match zoom with
| None -> Jv.call map "setView" [| latlng |]
| Some zoom -> Jv.call map "setView" [| latlng; Jv.of_int zoom |]
let as_target map = Brr.Ev.target_of_jv map let as_target map = Brr.Ev.target_of_jv map
let click = Brr.Ev.Type.create (Jstr.v "click") let click = Brr.Ev.Type.create (Jstr.v "click")
(*?= let click = Brr.Ev.click *)
let on ~event ~handler map =
ignore @@ Jv.call map "on" [| Jv.of_string event; Jv.repr handler |]
let get_center map = Jv.call map "getCenter" [||]
let get_zoom map = Jv.call map "getZoom" [||] |> Jv.to_int
let wrapped_latlng latlng map = Jv.call map "wrapLatLng" [| latlng |]
end end
module TileLayer = struct module TileLayer = struct
@ -53,25 +71,33 @@ module TileLayer = struct
let create_osm () = let create_osm () =
Jv.call leaflet "tileLayer" Jv.call leaflet "tileLayer"
[| [| Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"; ; Jv.obj
Jv.obj [| ( "attribution"
[| , Jv.of_string
( "attribution", "&copy; <a \
Jv.of_string href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \
"&copy; <a \ contributors" )
href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \ |]
contributors" );
|];
|] |]
let add_to tile_layer map = ignore @@ Jv.call tile_layer "addTo" [| map |] let add_to tile_layer map = ignore @@ Jv.call tile_layer "addTo" [| map |]
end end
module GeojsonLayer = struct
type t = Jv.t
let create ?(options = Jv.null) geojson =
Jv.call leaflet "geoJSON" [| geojson; options |]
let add_to layer map = ignore @@ Jv.call layer "addTo" [| map |]
end
module Marker = struct module Marker = struct
type t = Jv.t type t = Jv.t
let create latlng = Jv.call leaflet "marker" [| latlng |] let create latlng = Jv.call leaflet "marker" [| latlng |]
let add_to marker map = ignore @@ Jv.call marker "addTo" [| map |] let add_to marker map = ignore @@ Jv.call marker "addTo" [| map |]
let bind_popup el marker = let bind_popup el marker =
@ -81,3 +107,15 @@ module Marker = struct
let open_popup marker = ignore @@ Jv.call marker "openPopup" [||] let open_popup marker = ignore @@ Jv.call marker "openPopup" [||]
end end
module Popup = struct
let popup = Jv.call leaflet "popup" [||]
let set_latlng latlng = ignore @@ Jv.call popup "setLatLng" [| latlng |]
let set_content content =
ignore @@ Jv.call popup "setContent" [| Jv.of_string content |]
let open_on map = ignore @@ Jv.call popup "openOn" [| map |]
let close map = ignore @@ Jv.call map "closePopup" [||]
end

View file

@ -4,21 +4,24 @@
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
*) *)
open Brr
(** {1 Leaflet} (** {1 Leaflet}
This module provides bindings to the Leaflet JavaScript library for This module provides bindings to the Leaflet JavaScript library for
mobile-friendly interactive maps. mobile-friendly interactive maps.
See also the [Leaflet API reference](https://leafletjs.com/reference.html). See also the [Leaflet API reference](https://leafletjs.com/reference.html). *)
*) open Brr
module LatLng : sig module LatLng : sig
type t type t
val create : float -> float -> t val create : float -> float -> t
val lat : t -> float val lat : t -> float
val lng : t -> float val lng : t -> float
val equals : t -> t -> bool
end end
module Ev : sig module Ev : sig
@ -32,12 +35,24 @@ end
module Map : sig module Map : sig
type t type t
val create : ?options:Jv.t -> El.t -> t val create : ?options:Jv.t -> string -> t
val invalidate_size : t -> unit val invalidate_size : t -> unit
val set_view : LatLng.t -> zoom:int -> t -> t
val set_view : LatLng.t -> ?zoom:int -> t -> unit
val fit_world : t -> unit val fit_world : t -> unit
val get_container : t -> El.t val get_container : t -> El.t
val on : event:string -> handler:('a -> 'b) -> t -> unit
val get_center : t -> LatLng.t
val get_zoom : t -> int
val wrapped_latlng : LatLng.t -> t -> LatLng.t
(** {1 Events} **) (** {1 Events} **)
val as_target : t -> Brr.Ev.target val as_target : t -> Brr.Ev.target
@ -51,6 +66,15 @@ module TileLayer : sig
type t type t
val create_osm : unit -> t val create_osm : unit -> t
val add_to : t -> Map.t -> unit
end
module GeojsonLayer : sig
type t
val create : ?options:Jv.t -> Jv.t -> t
val add_to : t -> Map.t -> unit val add_to : t -> Map.t -> unit
end end
@ -58,10 +82,22 @@ module Marker : sig
type t type t
val create : LatLng.t -> t val create : LatLng.t -> t
val add_to : t -> Map.t -> unit val add_to : t -> Map.t -> unit
(** {2 Popup methods} *) (** {2 Popup methods} *)
val bind_popup : El.t -> t -> t val bind_popup : El.t -> t -> t
val open_popup : t -> unit val open_popup : t -> unit
end end
module Popup : sig
val set_latlng : LatLng.t -> unit
val set_content : string -> unit
val open_on : Map.t -> unit
val close : Map.t -> unit
end