Source

xJs / lib / gMaps.ml

Full commit
open Spotlib_js.Spot
open Js

open Std

module MapTypeId : sig
  type t = HYBIRD | ROADMAP | SATELLITE | TERRAIN
  val to_js : t -> Unsafe.any
end = struct
  type t = HYBIRD | ROADMAP | SATELLITE | TERRAIN
  let to_js = function
    | HYBIRD    -> Unsafe.variable "google.maps.MapTypeId.HYBRID"
    | ROADMAP   -> Unsafe.variable "google.maps.MapTypeId.ROADMAP"
    | SATELLITE -> Unsafe.variable "google.maps.MapTypeId.SATELLITE"
    | TERRAIN   -> Unsafe.variable "google.maps.MapTypeId.TERRAIN"
end

module LatLng = struct
  class type t = object
  end
end 

module MapOptions = struct
  type t
  include OptionFields.Make(struct type key = string type value = Unsafe.any end)
  let center (v : LatLng.t Js.t)  = make "center" Unsafe.inject v
  let zoom   (v : int)            = make "zoom"   Unsafe.inject v
  let mapTypeId (v : MapTypeId.t) = make "mapTypeId" (Unsafe.inject ** MapTypeId.to_js) v
  let run opts : t = run opts |> Array.of_list |> Unsafe.obj
end

module Map = struct

  class type t= object
  end
end

module MarkerOptions = struct
  type t
  include OptionFields.Make(struct type key = string type value = Unsafe.any end)
  let position (v : LatLng.t Js.t)  = make "position" Unsafe.inject v
  let map      (v : Map.t Js.t)     = make "map"      Unsafe.inject v
  let title    (v : js_string Js.t) = make "title"    Unsafe.inject v
  let run opts : t = run opts |> Array.of_list |> Unsafe.obj
end
module Marker = struct
  class type t = object
  end
end

class type t = object
  method _LatLng : (float -> float -> LatLng.t Js.t) constr readonly_prop
  method _Map : (Dom_html.element Js.t -> MapOptions.t -> Map.t Js.t) constr readonly_prop
  method _Marker : (MarkerOptions.t -> Marker.t Js.t) constr readonly_prop
end

let load ?sensor ?callback key = 
  let q = 
    URL.make_query 
    & List.filter_map (fun (k,v) -> Option.map ~f:(fun v -> (k, v)) v)
      [ "key", Some key;
        "sensor", Option.map ~f:(function true -> "true" | false -> "false") sensor;
        "callback", Option.map ~f:id callback]
  in
  Base.load_script (Printf.sprintf "http://maps.googleapis.com/maps/api/js?%s" q)

let get () : t Js.t = Unsafe.variable "google.maps"

let latlng (maps : t Js.t) lat lng = jsnew (maps##_LatLng) (lat, lng)

let map (maps : t Js.t) div mapOptions = jsnew (maps##_Map) (div, mapOptions)

let marker (maps : t Js.t) markerOptions = jsnew (maps##_Marker) (markerOptions)