Skip to content

Commit 8299cd3

Browse files
committed
Cleanup
1 parent 749d43c commit 8299cd3

File tree

5 files changed

+62
-65
lines changed

5 files changed

+62
-65
lines changed

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,27 @@ module Coordinates =
1212
abstract member lat: unit -> float
1313
abstract member lng: unit -> float
1414

15-
type [<Pojo>] Position = {
16-
lat: float
17-
lng: float
18-
}
15+
[<Emit("new window.google.maps.LatLng($0, $1)")>]
16+
let newLatLng (lat: float, lng: float) : LatLng = jsNative
1917

20-
let newPos lat lng =
21-
{ lat = lat
22-
lng = lng }
2318

24-
type [<Pojo>] Bounds = {
25-
NE : Position
26-
SW : Position
27-
}
28-
2919
// https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngBounds
3020

3121
type LatLngBounds =
32-
abstract member extend : U2<LatLngBounds, Position> -> LatLngBounds
33-
34-
[<Emit("new window.google.maps.LatLngBounds()")>]
35-
let newLatLngBounds () : LatLngBounds = jsNative
22+
abstract member extend : LatLngBounds -> LatLngBounds
23+
abstract member extend : LatLng -> LatLngBounds
3624

3725
[<Emit("new window.google.maps.LatLngBounds($0, $1)")>]
38-
let newLatLngBoundsWith (sw: U2<LatLng, Position>, ne: U2<LatLng, Position>) : LatLngBounds = jsNative
26+
let newLatLngBounds (sw: LatLng, ne: LatLng) : LatLngBounds = jsNative
27+
28+
29+
[<Emit("new window.google.maps.LatLngBounds()")>]
30+
let newEmptyLatLngBounds () : LatLngBounds = jsNative
3931

4032
module Places =
4133

4234
type [<Pojo>] Geometry = {
43-
location: Coordinates.Position
35+
location: Coordinates.LatLng
4436
}
4537

4638
type [<Pojo>] Place = {
@@ -55,22 +47,28 @@ type RCom = React.ComponentClass<obj>
5547
type MapRef(mapRef) =
5648

5749
/// Get the current bounds of the Map
58-
member __.GetBounds() : Coordinates.Bounds =
59-
let bounds = mapRef?getBounds()
60-
let ne = bounds?getNorthEast()
61-
let sw = bounds?getSouthWest()
50+
member __.GetBounds() : Coordinates.LatLngBounds option =
51+
if isNull mapRef then
52+
None
53+
else
54+
Some(mapRef?getBounds() |> unbox)
6255

63-
{ NE = Coordinates.newPos (ne?lat() |> unbox) (ne?lng() |> unbox)
64-
SW = Coordinates.newPos (sw?lat() |> unbox) (sw?lng() |> unbox) }
56+
member __.GetZoom() : int option =
57+
if isNull mapRef then
58+
None
59+
else
60+
Some(mapRef?getZoom() |> unbox)
6561

66-
member __.GetZoom() : int =
67-
mapRef?getZoom() |> unbox
6862

69-
member __.GetCenter() : Coordinates.Position =
70-
mapRef?getCenter() |> unbox
71-
72-
member __.FitBounds(bounds: U2<Coordinates.LatLngBounds, Coordinates.Bounds>, ?padding: float) : unit =
73-
mapRef?fitBounds(bounds, padding)
63+
member __.GetCenter() : Coordinates.LatLng option =
64+
if isNull mapRef then
65+
None
66+
else
67+
Some(mapRef?getCenter() |> unbox)
68+
69+
member __.FitBounds(bounds: Coordinates.LatLngBounds, ?padding: float) : unit =
70+
if not(isNull mapRef) then
71+
mapRef?fitBounds(bounds, padding) |> ignore
7472

7573
module Props =
7674

@@ -100,7 +98,7 @@ module Props =
10098
| Title of string
10199
| Icon of string
102100
| OnClick of (unit -> unit)
103-
| Position of Coordinates.Position
101+
| Position of Coordinates.LatLng
104102
interface IMarkerProperties
105103

106104
type IMarkerClustererProperties =
@@ -124,14 +122,14 @@ module Props =
124122

125123
[<RequireQualifiedAccess>]
126124
type MapProperties =
127-
| SetRef of (obj -> unit)
125+
| OnMapMounted of (obj -> unit)
128126
| ApiKey of string
129127
| DefaultZoom of int
130128
| SearchBoxText of string
131129
| ShowSearchBox of bool
132130
| ShowTrafficLayer of bool
133-
| DefaultCenter of Coordinates.Position
134-
| Center of Coordinates.Position
131+
| DefaultCenter of Coordinates.LatLng
132+
| Center of Coordinates.LatLng
135133
| OnCenterChanged of (unit -> unit)
136134
| OnPlacesChanged of (Places.Place [] -> unit)
137135
| OnZoomChanged of (unit -> unit)

src/Fable.ReactGoogleMaps/Fable.ReactGoogleMaps.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<Version>0.3.1</Version>
4+
<Version>0.4.0</Version>
55
<TargetFramework>netstandard2.0</TargetFramework>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
</PropertyGroup>

src/Fable.ReactGoogleMaps/README.md

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ It's possible to retrieve properties like current center or current bounds from
117117
```fs
118118
119119
let mutable mapRef = MapRef null
120-
let setMapRef (ref:obj) =
120+
let onMapMounted (ref:obj) =
121121
mapRef <- MapRef ref
122122
123123
// ...
@@ -127,7 +127,7 @@ let myMap =
127127
googleMap [
128128
MapProperties.ApiKey googleMapApiKey
129129
// ..
130-
MapProperties.SetRef setMapRef ]
130+
MapProperties.OnMapMounted onMapMounted ]
131131
132132
// ...
133133
@@ -138,29 +138,24 @@ let bounds = mapRef.GetBounds()
138138
### Set bounds of map to fit all the markers
139139

140140
```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 Option.ofObj ref with
149-
| Some ref ->
150-
markerPositions
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 ]
141+
// ...
142+
let markerPositions: Position list = // ...
143+
144+
145+
let mutable mapRef = MapRef null
146+
let onMapMounted (ref:obj) =
147+
mapRef <- MapRef ref
148+
149+
let bounds = ReactGoogleMaps.Coordinates.newEmptyLatLngBounds()
150+
151+
markerPositions
152+
|> List.fold (fun (acc:LatLngBounds) pos -> acc.extend pos) bounds
153+
154+
let myMap =
155+
googleMap [
156+
MapProperties.ApiKey googleMapApiKey
157+
// ..
158+
MapProperties.Markers markers
159+
MapProperties.OnMapMounted onMapMounted ]
160+
166161
```
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
### 0.3.1
1+
### 0.4.1
2+
3+
* Cleaning up map bounds
4+
5+
### 0.1.0
26

37
* Initial release

src/Fable.ReactGoogleMaps/mapComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class GoogleMapComponent extends React.PureComponent {
9292
this.props.onPlacesChanged(this.searchBoxRef.getPlaces())
9393
},
9494
onIdle: this.props.onIdle,
95-
onMapMounted: this.props.setRef,
95+
onMapMounted: this.props.onMapMounted,
9696
onSearchBoxMounted: ref => {
9797
this.searchBoxRef = ref;
9898
},

0 commit comments

Comments
 (0)