Skip to content

Commit c45b16f

Browse files
committed
Adding options and onClick for map.
1 parent ee9cce1 commit c45b16f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Fable.ReactGoogleMaps/Fable.Helpers.ReactGoogleMaps.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ module Coordinates =
2525
NE : Position
2626
SW : Position
2727
}
28-
28+
29+
// https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngBounds
30+
31+
type LatLngBounds =
32+
abstract member extend : U2<LatLngBounds, Position> -> LatLngBounds
33+
34+
[<Emit("new window.google.maps.LatLngBounds()")>]
35+
let newLatLngBounds () : LatLngBounds = jsNative
36+
37+
[<Emit("new window.google.maps.LatLngBounds($0, $1)")>]
38+
let newLatLngBoundsWith (sw: U2<LatLng, Position>, ne: U2<LatLng, Position>) : LatLngBounds = jsNative
2939

3040
module Places =
3141

@@ -58,6 +68,9 @@ type MapRef(mapRef) =
5868

5969
member __.GetCenter() : Coordinates.Position =
6070
mapRef?getCenter() |> unbox
71+
72+
member __.FitBounds(bounds: Coordinates.LatLngBounds) : unit =
73+
mapRef?fitBounds(bounds)
6174

6275
module Props =
6376

src/Fable.ReactGoogleMaps/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,33 @@ let myMap =
134134
let bounds = mapRef.GetBounds()
135135
136136
```
137+
138+
### Set bounds of map to fit all the markers
139+
140+
```fs
141+
// ...
142+
let markerPositions: Position list = // ...
143+
144+
// In some scenarios ref can be null, f.ex. slow network when the google maps isn't fully loaded yet.
145+
let setRef (ref:obj) =
146+
let bounds = ReactGoogleMaps.Coordinates.newLatLngBounds()
147+
148+
match markerPositions, Option.ofObj ref with
149+
| multiple, Some ref ->
150+
multiple
151+
|> List.fold (fun (acc:LatLngBounds) pos ->
152+
acc.extend( !^ pos)
153+
) bounds
154+
|> (MapRef ref).FitBounds
155+
| _ ->
156+
()
157+
158+
googleMap [ MapProperties.ApiKey googleMapApiKey
159+
MapProperties.MapContainer "mapContainer"
160+
MapProperties.DefaultZoom 9
161+
MapProperties.DefaultCenter defaultCenter
162+
MapProperties.Center defaultCenter
163+
MapProperties.Options mapStyle
164+
MapProperties.Markers markers
165+
MapProperties.SetRef setRef ]
166+
```

0 commit comments

Comments
 (0)