Skip to content

Commit 8daf49c

Browse files
committed
getting regions working
also adjusting some props and adding error handling for center and span.
1 parent 6c65253 commit 8daf49c

File tree

3 files changed

+115
-33
lines changed

3 files changed

+115
-33
lines changed

flow-typed/mapkit.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ declare module 'mapkit' {
1313
minimumSpan?: CoordinateSpan,
1414
}
1515

16-
declare type CoordinateRegion = {
17-
//todo
18-
}
19-
declare type CoordinateSpan = {
20-
//todo
21-
}
2216
declare type MapRect = {
2317
//todo
2418
}
@@ -34,6 +28,9 @@ declare module 'mapkit' {
3428
declare type TileOverlay = {
3529
//todo
3630
}
31+
declare type BoundingRegion = {
32+
//todo
33+
}
3734

3835
declare type MapKitInitOptions = {
3936
language?: string,
@@ -89,7 +86,7 @@ declare module 'mapkit' {
8986
toCoordinate: () => Coordinate,
9087
}
9188

92-
declare class Coordinate {
89+
declare export class Coordinate {
9390
latitude: number;
9491
longitude: number;
9592

@@ -99,6 +96,24 @@ declare module 'mapkit' {
9996
toUnwrappedMapPoint: () => MapPoint;
10097
}
10198

99+
declare export class CoordinateSpan {
100+
latitudeDelta: number;
101+
longitudeDelta: number;
102+
103+
copy: () => CoordinateSpan;
104+
equals: (CoordinateSpan) => boolean;
105+
}
106+
107+
declare export class CoordinateRegion {
108+
center: Coordinate;
109+
span: CoordinateSpan;
110+
111+
copy: () => CoordinateRegion;
112+
equals: (CoordinateRegion) => boolean;
113+
toBoundingRegion: () => BoundingRegion;
114+
toMapRect: () => MapRect;
115+
}
116+
102117
declare class Padding {
103118
top: number;
104119
right: number;
@@ -196,5 +211,13 @@ declare module 'mapkit' {
196211
Map(domId?: string, ?MapConstructorOptions): Map;
197212
Padding(PaddingOptions): Padding;
198213
Coordinate(latitude: number, longitude: number): Coordinate;
214+
CoordinateSpan(
215+
latitudeDelta: number,
216+
longitudeDelta: number,
217+
): CoordinateSpan;
218+
CoordinateRegion(
219+
center: Coordinate,
220+
span: CoordinateSpan,
221+
): CoordinateRegion;
199222
}
200223
}

src/index.js

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type MapKitType, {
55
MapType,
66
Map,
77
PaddingOptions,
8+
Coordinate,
9+
CoordinateSpan,
810
} from 'mapkit'
911
declare var mapkit: MapKitType
1012

@@ -14,16 +16,31 @@ import invariant from 'invariant'
1416

1517
import ErrorBoundry from './ErrorBoundry'
1618

17-
type MapKitCoordinate = [number, number]
18-
19-
type Location = MapKitCoordinate
20-
19+
type NumberTuple = [number, number]
2120
type PaddingType = number | PaddingOptions
2221

2322
type Props = {
23+
// Init Props
24+
// ⚠️ These props are used for setup and can't be changed once set.
25+
// ⚠️ Pick between callbackUrl or token.
2426
callbackUrl?: string,
2527
token?: string,
2628

29+
// Interaction Properties
30+
isRotationEnabled: boolean,
31+
isScrollEnabled: boolean,
32+
isZoomEnabled: boolean,
33+
34+
// Manipulating the Visible Portion of the Map
35+
center?: NumberTuple,
36+
span?: NumberTuple,
37+
animateViewChange: boolean,
38+
rotation?: number,
39+
animateRotationChange: boolean,
40+
// visibleMapRect
41+
// animateMapRectChange
42+
43+
// Configuring the Map's Appearance
2744
mapType: MapType,
2845
padding: PaddingType,
2946
showsCompass: FeatureVisibility,
@@ -34,18 +51,18 @@ type Props = {
3451
showsScale: FeatureVisibility,
3552
tintColor?: string,
3653

37-
center?: MapKitCoordinate,
38-
animateCenterChange: boolean,
54+
// Annotations
55+
// todo
3956

40-
isRotationEnabled: boolean,
41-
isScrollEnabled: boolean,
42-
isZoomEnabled: boolean,
57+
// Overlays
58+
// todo
59+
60+
// TileOverlays
61+
// todo
4362

63+
// Displaying the User's Location
4464
showsUserLocation: boolean,
4565
tracksUserLocation: boolean,
46-
47-
rotation?: number,
48-
animateRotationChange: boolean,
4966
}
5067

5168
type State = {
@@ -141,19 +158,48 @@ class MapKit extends React.Component<Props, State> {
141158
this.map.showsUserLocation = props.showsUserLocation
142159
this.map.tracksUserLocation = props.tracksUserLocation
143160

144-
const newCenter = props.center
145-
? this.createCoordinate(props.center)
146-
: this.createCoordinate([0, 0])
161+
// Map Center
162+
let newCenter = this.createCoordinate(0, 0)
163+
let newSpan
147164

148-
if (!newCenter.equals(this.map.center)) {
149-
this.map.setCenterAnimated(newCenter, props.animateCenterChange)
165+
if (props.center) {
166+
try {
167+
newCenter = this.createCoordinate(props.center[0], props.center[1])
168+
} catch (e) {
169+
console.warn(e.message)
170+
}
150171
}
151172

173+
if (props.span) {
174+
try {
175+
newSpan = this.createCoordinateSpan(props.span[0], props.span[1])
176+
} catch (e) {
177+
console.warn(e.message)
178+
}
179+
}
180+
181+
if (newSpan) {
182+
// if we have a span we'll set a region
183+
const newRegion = this.createCoordinateRegion(newCenter, newSpan)
184+
185+
if (!newRegion.equals(this.map.region)) {
186+
this.map.setRegionAnimated(newRegion, props.animateViewChange)
187+
}
188+
} else {
189+
// otherwise we just set the center
190+
if (!newCenter.equals(this.map.center)) {
191+
this.map.setCenterAnimated(newCenter, props.animateViewChange)
192+
}
193+
}
194+
195+
// Map Rotation
152196
const newRotation = props.rotation ? props.rotation : 0
153197

154198
if (!this.map.rotation !== newRotation) {
155199
this.map.setRotationAnimated(newRotation, props.animateRotationChange)
156200
}
201+
202+
// Map Region
157203
}
158204

159205
createPadding = (padding: PaddingType) => {
@@ -169,8 +215,16 @@ class MapKit extends React.Component<Props, State> {
169215
)
170216
}
171217

172-
createCoordinate = (location: Location) => {
173-
return new mapkit.Coordinate(location[0], location[1])
218+
createCoordinate = (latitude: number, longitude: number) => {
219+
return new mapkit.Coordinate(latitude, longitude)
220+
}
221+
222+
createCoordinateSpan = (latitudeDelta: number, longitudeDelta: number) => {
223+
return new mapkit.CoordinateSpan(latitudeDelta, longitudeDelta)
224+
}
225+
226+
createCoordinateRegion = (center: Coordinate, span: CoordinateSpan) => {
227+
return new mapkit.CoordinateRegion(center, span)
174228
}
175229

176230
shouldComponentUpdate(nextProps: Props, nextState: State) {
@@ -219,8 +273,12 @@ class MapKit extends React.Component<Props, State> {
219273
showsScale,
220274
tintColor,
221275

222-
animateCenterChange,
223276
center,
277+
span,
278+
animateViewChange,
279+
280+
rotation,
281+
animateRotationChange,
224282

225283
isRotationEnabled,
226284
isScrollEnabled,
@@ -229,9 +287,6 @@ class MapKit extends React.Component<Props, State> {
229287
showsUserLocation,
230288
tracksUserLocation,
231289

232-
rotation,
233-
animateRotationChange,
234-
235290
...otherProps
236291
} = this.props
237292

stories/index.stories.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ storiesOf('MapKit', module)
3333
)}
3434
tintColor={text('tintColor', '')}
3535
center={[
36-
number('latitude', 47.6063889),
37-
number('longitude', -122.3308333),
36+
number('center latitude', 47.6063889),
37+
number('center longitude', -122.3308333),
3838
]}
39-
animateCenterChange={boolean('animateCenterChange', true)}
39+
span={[
40+
number('span latitude delta', 0.016),
41+
number('span longitude delta', 0.016),
42+
]}
43+
animateViewChange={boolean('animateViewChange', true)}
4044
isRotationEnabled={boolean('isRotationEnabled', true)}
4145
isScrollEnabled={boolean('isScrollEnabled', true)}
4246
isZoomEnabled={boolean('isZoomEnabled', true)}

0 commit comments

Comments
 (0)