diff --git a/src/leaflet.ml b/src/leaflet.ml index e1c5948..fca38f9 100644 --- a/src/leaflet.ml +++ b/src/leaflet.ml @@ -17,7 +17,10 @@ module LatLng = struct Jv.call leaflet "latLng" [| Jv.of_float lat; Jv.of_float lng |] let lat latlng = Jv.get latlng "lat" |> 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 module Ev = struct @@ -31,21 +34,36 @@ end module Map = struct type t = Jv.t - let create ?(options = Jv.null) el = - Jv.call leaflet "map" [| El.to_jv el; options |] + let create ?(options = Jv.null) container_id = + Jv.call leaflet "map" [| Jv.of_string container_id; options |] let invalidate_size map = ignore @@ Jv.call map "invalidateSize" [| Jv.true' |] let fit_world map = ignore @@ Jv.call map "fitWorld" [||] + let get_container map = Jv.call map "getContainer" [||] |> El.of_jv - let set_view latlng ~zoom map = - ignore @@ Jv.call map "setView" [| latlng; Jv.of_int zoom |]; - map + let set_view latlng ?zoom map = + ignore + @@ + 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 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 module TileLayer = struct @@ -53,25 +71,33 @@ module TileLayer = struct let create_osm () = Jv.call leaflet "tileLayer" - [| - Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"; - Jv.obj - [| - ( "attribution", - Jv.of_string - "© OpenStreetMap \ - contributors" ); - |]; + [| Jv.of_string "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" + ; Jv.obj + [| ( "attribution" + , Jv.of_string + "© OpenStreetMap \ + contributors" ) + |] |] let add_to tile_layer map = ignore @@ Jv.call tile_layer "addTo" [| map |] 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 type t = Jv.t let create latlng = Jv.call leaflet "marker" [| latlng |] + let add_to marker map = ignore @@ Jv.call marker "addTo" [| map |] let bind_popup el marker = @@ -81,3 +107,15 @@ module Marker = struct let open_popup marker = ignore @@ Jv.call marker "openPopup" [||] 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 diff --git a/src/leaflet.mli b/src/leaflet.mli index 16f36fc..dd166cf 100644 --- a/src/leaflet.mli +++ b/src/leaflet.mli @@ -4,21 +4,24 @@ * SPDX-License-Identifier: AGPL-3.0-or-later *) -open Brr (** {1 Leaflet} -This module provides bindings to the Leaflet JavaScript library for -mobile-friendly interactive maps. + This module provides bindings to the Leaflet JavaScript library for + 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 type t val create : float -> float -> t + val lat : t -> float + val lng : t -> float + + val equals : t -> t -> bool end module Ev : sig @@ -32,12 +35,24 @@ end module Map : sig type t - val create : ?options:Jv.t -> El.t -> t + val create : ?options:Jv.t -> string -> t + 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 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} **) val as_target : t -> Brr.Ev.target @@ -51,6 +66,15 @@ module TileLayer : sig type 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 end @@ -58,10 +82,22 @@ module Marker : sig type t val create : LatLng.t -> t + val add_to : t -> Map.t -> unit (** {2 Popup methods} *) val bind_popup : El.t -> t -> t + val open_popup : t -> unit 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