|
| 1 | +module Fable.Helpers.ReactGoogleMaps |
| 2 | + |
| 3 | +open Fable.Core |
| 4 | +open Fable.Import |
| 5 | +open Fable.Core.JsInterop |
| 6 | + |
| 7 | +module Coordinates = |
| 8 | + |
| 9 | + type [<Pojo>] Position = { |
| 10 | + lat: float |
| 11 | + lng: float |
| 12 | + } |
| 13 | + |
| 14 | + let newPos lat lng = |
| 15 | + { lat = lat |
| 16 | + lng = lng } |
| 17 | + |
| 18 | + type [<Pojo>] Bounds = { |
| 19 | + NE : Position |
| 20 | + SW : Position |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | +module Places = |
| 25 | + |
| 26 | + type [<Pojo>] Geometry = { |
| 27 | + location: Coordinates.Position |
| 28 | + } |
| 29 | + |
| 30 | + type [<Pojo>] Place = { |
| 31 | + geometry: Geometry |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +module R = Fable.Helpers.React |
| 36 | + |
| 37 | +type RCom = React.ComponentClass<obj> |
| 38 | + |
| 39 | +type MapRef(mapRef) = |
| 40 | + |
| 41 | + /// Get the current bounds of the Map |
| 42 | + member __.GetBounds() : Coordinates.Bounds = |
| 43 | + let bounds = mapRef?getBounds() |
| 44 | + let ne = bounds?getNorthEast() |
| 45 | + let sw = bounds?getSouthWest() |
| 46 | + |
| 47 | + { NE = Coordinates.newPos (ne?lat() |> unbox) (ne?lng() |> unbox) |
| 48 | + SW = Coordinates.newPos (sw?lat() |> unbox) (sw?lng() |> unbox) } |
| 49 | + |
| 50 | + member __.GetZoom() : int = |
| 51 | + mapRef?getZoom() |> unbox |
| 52 | + |
| 53 | + member __.GetCenter() : Coordinates.Position = |
| 54 | + mapRef?getCenter() |> unbox |
| 55 | + |
| 56 | +module Props = |
| 57 | + |
| 58 | + |
| 59 | + type IInfoWindowProperties = |
| 60 | + interface end |
| 61 | + |
| 62 | + [<RequireQualifiedAccess>] |
| 63 | + type InfoWindowProperties = |
| 64 | + | OnCloseClick of (unit -> unit) |
| 65 | + interface IInfoWindowProperties |
| 66 | + |
| 67 | + type ISearchBoxProperties = |
| 68 | + interface end |
| 69 | + |
| 70 | + [<RequireQualifiedAccess>] |
| 71 | + type SearchBoxProperties = |
| 72 | + | Ref of (obj -> unit) |
| 73 | + | OnPlacesChanged of (unit -> unit) |
| 74 | + interface ISearchBoxProperties |
| 75 | + |
| 76 | + type IMarkerProperties = |
| 77 | + interface end |
| 78 | + |
| 79 | + [<RequireQualifiedAccess>] |
| 80 | + type MarkerProperties = |
| 81 | + | Key of obj |
| 82 | + | Title of string |
| 83 | + | Icon of string |
| 84 | + | OnClick of (unit -> unit) |
| 85 | + | Position of Coordinates.Position |
| 86 | + interface IMarkerProperties |
| 87 | + |
| 88 | + type IMarkerClustererProperties = |
| 89 | + interface end |
| 90 | + |
| 91 | + [<RequireQualifiedAccess>] |
| 92 | + type MarkerClustererProperties = |
| 93 | + | Key of obj |
| 94 | + | AverageCenter of bool |
| 95 | + | MaxZoom of int |
| 96 | + | EnableRetinaIcons of bool |
| 97 | + | GridSize of float |
| 98 | + interface IMarkerClustererProperties |
| 99 | + |
| 100 | + type IMapProperties = |
| 101 | + interface end |
| 102 | + |
| 103 | + [<RequireQualifiedAccess>] |
| 104 | + type MapProperties = |
| 105 | + | SetRef of (obj -> unit) |
| 106 | + | ApiKey of string |
| 107 | + | DefaultZoom of int |
| 108 | + | SearchBoxText of string |
| 109 | + | ShowSearchBox of bool |
| 110 | + | ShowTrafficLayer of bool |
| 111 | + | DefaultCenter of Coordinates.Position |
| 112 | + | Center of Coordinates.Position |
| 113 | + | OnCenterChanged of (unit -> unit) |
| 114 | + | OnPlacesChanged of (Places.Place [] -> unit) |
| 115 | + | OnZoomChanged of (unit -> unit) |
| 116 | + | OnIdle of (unit -> unit) |
| 117 | + | Markers of React.ReactElement |
| 118 | + | MapLoadingContainer of string |
| 119 | + | MapContainer of string |
| 120 | + interface IMapProperties |
| 121 | +let InfoWindow: RCom = import "InfoWindow" "react-google-maps" |
| 122 | + |
| 123 | +/// A wrapper around google.maps.InfoWindow |
| 124 | +let infoWindow (props:Props.IInfoWindowProperties list) child : React.ReactElement = |
| 125 | + R.from InfoWindow (keyValueList CaseRules.LowerFirst props) [child] |
| 126 | + |
| 127 | +let SearchBox: RCom = import "SearchBox" "react-google-maps" |
| 128 | + |
| 129 | +/// A wrapper around google.maps.places.SearchBox on the map |
| 130 | +let searchBox (props:Props.ISearchBoxProperties list) children : React.ReactElement = |
| 131 | + R.from SearchBox (keyValueList CaseRules.LowerFirst props) children |
| 132 | + |
| 133 | + |
| 134 | +let Marker: RCom = import "Marker" "react-google-maps" |
| 135 | + |
| 136 | +/// A wrapper around google.maps.Marker |
| 137 | +let marker (props:Props.IMarkerProperties list) children : React.ReactElement = |
| 138 | + R.from Marker (keyValueList CaseRules.LowerFirst props) children |
| 139 | + |
| 140 | +let MarkerClusterer: RCom = import "MarkerClusterer" "react-google-maps/lib/components/addons/MarkerClusterer.js" |
| 141 | + |
| 142 | +/// A wrapper around MarkerClusterer - https://github.com/mahnunchik/markerclustererplus/blob/master/docs/reference.html |
| 143 | +let markerClusterer (props:Props.IMarkerClustererProperties list) (markers:React.ReactElement list) : React.ReactElement = |
| 144 | + R.from MarkerClusterer (keyValueList CaseRules.LowerFirst props) markers |
| 145 | + |
| 146 | +let GoogleMapComponent: RCom = import "GoogleMapComponent" "./mapComponent.js" |
| 147 | + |
| 148 | + |
| 149 | +/// A wrapper around google.maps.Map |
| 150 | +let googleMap (props:Props.IMapProperties list) : React.ReactElement = |
| 151 | + |
| 152 | + R.from GoogleMapComponent (keyValueList CaseRules.LowerFirst props) [] |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +let getPosition (options: obj) : JS.Promise<obj> = importMember "./location.js" |
| 157 | + |
| 158 | +open Fable.PowerPack |
| 159 | + |
| 160 | +let getGeoPosition () = |
| 161 | + promise { |
| 162 | + let! pos = getPosition() |
| 163 | + let c = pos?coords |
| 164 | + return Coordinates.newPos (c?latitude |> unbox) (c?longitude |> unbox) |
| 165 | + } |
| 166 | + |
0 commit comments