From a438e54f42afc995819dbf5f143a388e8b3e711b Mon Sep 17 00:00:00 2001 From: Swrup Date: Mon, 20 Jun 2022 09:17:23 +0200 Subject: [PATCH] add geojson options --- src/layer.ml | 36 +++++++++++++++++++++++++++++++++--- src/layer.mli | 15 +++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/layer.ml b/src/layer.ml index 7f34d54..b4261aa 100644 --- a/src/layer.ml +++ b/src/layer.ml @@ -78,9 +78,39 @@ let on : type kind. kind Event.sub -> (kind Event.t -> 'b) -> 'c t -> unit = (** 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 +type geojson_opt = + | Point_to_layer of (Jv.t -> Latlng.t -> unit) + | Style of (Jv.t -> unit) + | On_each_feature of (Jv.t -> [ `Geojson ] t -> unit) + | Filter of (Jv.t -> bool) + | Coords_to_latlng of (Jv.t -> Latlng.t) + | Markers_inherit_options of bool + +let geojson_opt_to_string = function + | Point_to_layer _ -> "pointToLayer" + | Style _ -> "style" + | On_each_feature _ -> "onEachFeature" + | Filter _ -> "filter" + | Coords_to_latlng _ -> "coordsToLatLng" + | Markers_inherit_options _ -> "markersInheritOptions" + +let geojson_opt_to_jv = function + | Markers_inherit_options b -> Jv.of_bool b + | Point_to_layer f -> Jv.repr f + | Style f -> Jv.repr f + | On_each_feature f -> Jv.repr f + | Filter f -> Jv.repr f + | Coords_to_latlng f -> Jv.repr f + +let create_geojson : Jv.t -> geojson_opt list -> [ `Geojson ] t = + fun geojson options -> + let l = + Array.of_list + @@ List.map + (fun o -> (geojson_opt_to_string o, geojson_opt_to_jv o)) + options + in + let jv_t = Jv.call Global.leaflet "geoJSON" [| geojson; Jv.obj l |] in Geojson jv_t (** Marker layers *) diff --git a/src/layer.mli b/src/layer.mli index 9f85a0a..204afab 100644 --- a/src/layer.mli +++ b/src/layer.mli @@ -50,11 +50,21 @@ val on : 'a Event.sub -> ('a Event.t -> 'b) -> 'c t -> unit (** Geojson layers *) +(** type for geojson option, used to create geojson *) +type geojson_opt = + | Point_to_layer of (Jv.t -> Latlng.t -> unit) + | Style of (Jv.t -> unit) + | On_each_feature of (Jv.t -> [ `Geojson ] t -> unit) + | Filter of (Jv.t -> bool) + | Coords_to_latlng of (Jv.t -> Latlng.t) + | Markers_inherit_options of bool + (** [create_geojson geojson] is a new geojson layer *) -val create_geojson : ?options:Jv.t -> Jv.t -> [ `Geojson ] t +val create_geojson : Jv.t -> geojson_opt list -> [ `Geojson ] t (** Marker layers *) +(** type for marker option, used to create marker *) type marker_opt = | Icon of Icon.t | Keyboard of bool @@ -69,7 +79,8 @@ type marker_opt = | Bubbling_mouse_events of bool | Auto_pan_on_focus of bool -(** [create_marker latlng] is a new marker with the same position as latlng *) +(** [create_marker latlng options] is a new marker with the same position as + [latlng] and with options set to [options] *) val create_marker : Latlng.t -> marker_opt list -> [ `Marker ] t (** Tile layers *)