Skip to content

Commit 0e24380

Browse files
committed
getting mapRect up
- more flow - story adjustment
1 parent 8daf49c commit 0e24380

File tree

3 files changed

+138
-51
lines changed

3 files changed

+138
-51
lines changed

flow-typed/mapkit.js

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

16-
declare type MapRect = {
17-
//todo
18-
}
1916
declare type Annotation = {
2017
//todo
2118
}
@@ -77,13 +74,41 @@ declare module 'mapkit' {
7774
element: HTMLElement,
7875
}
7976

80-
declare type MapPoint = {
81-
x: number,
82-
y: number,
77+
declare export class MapPoint {
78+
x: number;
79+
y: number;
80+
81+
copy: () => MapPoint;
82+
equals: (MapPoint) => boolean;
83+
toCoordinate: () => Coordinate;
84+
}
85+
86+
declare export class MapSize {
87+
width: number;
88+
height: number;
89+
90+
copy: () => MapSize;
91+
equals: (MapSize) => boolean;
92+
}
93+
94+
declare export class MapRect {
95+
origin: MapPoint;
96+
size: MapSize;
97+
98+
maxX: () => number;
99+
maxY: () => number;
100+
101+
midX: () => number;
102+
midY: () => number;
103+
104+
minX: () => number;
105+
minY: () => number;
106+
107+
copy: () => MapRect;
108+
equals: (MapRect) => boolean;
83109

84-
copy: () => MapPoint,
85-
equals: (MapPoint) => boolean,
86-
toCoordinate: () => Coordinate,
110+
scale: (scaleFactor: number, scaleCenter: MapPoint) => MapRect;
111+
toCoordinateRegion: () => CoordinateRegion;
87112
}
88113

89114
declare export class Coordinate {
@@ -219,5 +244,8 @@ declare module 'mapkit' {
219244
center: Coordinate,
220245
span: CoordinateSpan,
221246
): CoordinateRegion;
247+
MapPoint(x: number, y: number): MapPoint;
248+
MapRect(x: number, y: number, width: number, height: number): MapRect;
249+
MapSize(width: number, height: number): MapSize;
222250
}
223251
}

src/index.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import invariant from 'invariant'
1717
import ErrorBoundry from './ErrorBoundry'
1818

1919
type NumberTuple = [number, number]
20+
type Rect = [number, number, number, number]
2021
type PaddingType = number | PaddingOptions
2122

2223
type Props = {
@@ -34,11 +35,11 @@ type Props = {
3435
// Manipulating the Visible Portion of the Map
3536
center?: NumberTuple,
3637
span?: NumberTuple,
38+
mapRect?: Rect,
3739
animateViewChange: boolean,
3840
rotation?: number,
3941
animateRotationChange: boolean,
4042
// visibleMapRect
41-
// animateMapRectChange
4243

4344
// Configuring the Map's Appearance
4445
mapType: MapType,
@@ -84,7 +85,7 @@ class MapKit extends React.Component<Props, State> {
8485
showsUserLocationControl: false,
8586
showsPointsOfInterest: true,
8687
showsScale: 'hidden',
87-
animateCenterChange: true,
88+
animateViewChange: true,
8889
isRotationEnabled: true,
8990
isScrollEnabled: true,
9091
isZoomEnabled: true,
@@ -158,37 +159,55 @@ class MapKit extends React.Component<Props, State> {
158159
this.map.showsUserLocation = props.showsUserLocation
159160
this.map.tracksUserLocation = props.tracksUserLocation
160161

161-
// Map Center
162-
let newCenter = this.createCoordinate(0, 0)
163-
let newSpan
162+
// Map View Area
164163

165-
if (props.center) {
164+
if (props.mapRect) {
165+
let newRect
166166
try {
167-
newCenter = this.createCoordinate(props.center[0], props.center[1])
167+
newRect = this.createMapRect(
168+
props.mapRect[0],
169+
props.mapRect[1],
170+
props.mapRect[2],
171+
props.mapRect[3],
172+
)
168173
} catch (e) {
169174
console.warn(e.message)
170175
}
171-
}
172-
173-
if (props.span) {
174-
try {
175-
newSpan = this.createCoordinateSpan(props.span[0], props.span[1])
176-
} catch (e) {
177-
console.warn(e.message)
176+
if (newRect && !newRect.equals(this.map.visibleMapRect)) {
177+
this.map.setVisibleMapRectAnimated(newRect, props.animateViewChange)
178178
}
179-
}
179+
} else {
180+
let newCenter = this.createCoordinate(0, 0)
181+
let newSpan
182+
183+
if (props.center) {
184+
try {
185+
newCenter = this.createCoordinate(props.center[0], props.center[1])
186+
} catch (e) {
187+
console.warn(e.message)
188+
}
189+
190+
if (props.span) {
191+
try {
192+
newSpan = this.createCoordinateSpan(props.span[0], props.span[1])
193+
} catch (e) {
194+
console.warn(e.message)
195+
}
196+
}
180197

181-
if (newSpan) {
182-
// if we have a span we'll set a region
183-
const newRegion = this.createCoordinateRegion(newCenter, newSpan)
198+
if (newSpan) {
199+
// if we have a span we'll set a region
200+
const newRegion = this.createCoordinateRegion(newCenter, newSpan)
184201

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)
202+
if (!newRegion.equals(this.map.region)) {
203+
this.map.setRegionAnimated(newRegion, props.animateViewChange)
204+
}
205+
} else {
206+
// otherwise we just set the center
207+
if (!newCenter.equals(this.map.center)) {
208+
this.map.setCenterAnimated(newCenter, props.animateViewChange)
209+
}
210+
}
192211
}
193212
}
194213

@@ -227,6 +246,14 @@ class MapKit extends React.Component<Props, State> {
227246
return new mapkit.CoordinateRegion(center, span)
228247
}
229248

249+
createMapPoint = (x: number, y: number) => {
250+
return new mapkit.MapPoint(x, y)
251+
}
252+
253+
createMapRect = (x: number, y: number, width: number, height: number) => {
254+
return new mapkit.MapRect(x, y, width, height)
255+
}
256+
230257
shouldComponentUpdate(nextProps: Props, nextState: State) {
231258
// make sure we have at least one init prop
232259
this.checkProps(nextProps)
@@ -275,6 +302,7 @@ class MapKit extends React.Component<Props, State> {
275302

276303
center,
277304
span,
305+
mapRect,
278306
animateViewChange,
279307

280308
rotation,

stories/index.stories.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import devToken from '../devToken'
77
import MapKit from '../src'
88

99
storiesOf('MapKit', module)
10-
.add('all props', () => (
10+
.add('Map Controls', () => (
1111
<MapKit
1212
style={{ width: '100vw', height: '100vh' }}
13-
token={text('token', devToken)}
13+
token={devToken}
1414
mapType={select(
1515
'mapType',
1616
{ standard: 'standard', satellite: 'satellite', hybrid: 'hybrid' },
1717
'standard',
1818
)}
19-
padding={number('padding', 0)}
2019
showsCompass={select(
2120
'showsCompass',
2221
{ adaptive: 'adaptive', hidden: 'hidden', visible: 'visible' },
@@ -32,6 +31,44 @@ storiesOf('MapKit', module)
3231
'hidden',
3332
)}
3433
tintColor={text('tintColor', '')}
34+
isRotationEnabled={boolean('isRotationEnabled', true)}
35+
isScrollEnabled={boolean('isScrollEnabled', true)}
36+
isZoomEnabled={boolean('isZoomEnabled', true)}
37+
showsUserLocation={boolean('showsUserLocation', false)}
38+
tracksUserLocation={boolean('tracksUserLocation', false)}
39+
/>
40+
))
41+
.add('Map Padding (single)', () => (
42+
<MapKit
43+
style={{ width: '100vw', height: '100vh' }}
44+
token={devToken}
45+
padding={number('padding', 0)}
46+
/>
47+
))
48+
.add('Map Padding (individual)', () => (
49+
<MapKit
50+
style={{ width: '100vw', height: '100vh' }}
51+
token={devToken}
52+
padding={{
53+
top: number('padding top', 0),
54+
right: number('padding right', 0),
55+
bottom: number('padding bottom', 0),
56+
left: number('padding left', 0),
57+
}}
58+
/>
59+
))
60+
.add('Rotation', () => (
61+
<MapKit
62+
style={{ width: '100vw', height: '100vh' }}
63+
token={devToken}
64+
rotation={number('rotation', 0)}
65+
animateRotationChange={boolean('animateRotationChange', true)}
66+
/>
67+
))
68+
.add('View (Center and Span)', () => (
69+
<MapKit
70+
style={{ width: '100vw', height: '100vh' }}
71+
token={devToken}
3572
center={[
3673
number('center latitude', 47.6063889),
3774
number('center longitude', -122.3308333),
@@ -41,24 +78,18 @@ storiesOf('MapKit', module)
4178
number('span longitude delta', 0.016),
4279
]}
4380
animateViewChange={boolean('animateViewChange', true)}
44-
isRotationEnabled={boolean('isRotationEnabled', true)}
45-
isScrollEnabled={boolean('isScrollEnabled', true)}
46-
isZoomEnabled={boolean('isZoomEnabled', true)}
47-
showsUserLocation={boolean('showsUserLocation', false)}
48-
tracksUserLocation={boolean('tracksUserLocation', false)}
49-
rotation={number('rotation', 0)}
50-
animateRotationChange={boolean('animateRotationChange', true)}
5181
/>
5282
))
53-
.add('individual padding values', () => (
83+
.add('View (MapRect)', () => (
5484
<MapKit
5585
style={{ width: '100vw', height: '100vh' }}
5686
token={devToken}
57-
padding={{
58-
top: number('top', 0),
59-
right: number('right', 0),
60-
bottom: number('bottom', 0),
61-
left: number('left', 0),
62-
}}
87+
mapRect={[
88+
number('x', 0.155),
89+
number('y', 0.345),
90+
number('width', 0.03),
91+
number('height', 0.04),
92+
]}
93+
animateViewChange={boolean('animateViewChange', true)}
6394
/>
6495
))

0 commit comments

Comments
 (0)