@@ -5,6 +5,8 @@ import type MapKitType, {
55 MapType ,
66 Map ,
77 PaddingOptions ,
8+ Coordinate ,
9+ CoordinateSpan ,
810} from 'mapkit'
911declare var mapkit: MapKitType
1012
@@ -14,16 +16,31 @@ import invariant from 'invariant'
1416
1517import ErrorBoundry from './ErrorBoundry'
1618
17- type MapKitCoordinate = [ number , number ]
18-
19- type Location = MapKitCoordinate
20-
19+ type NumberTuple = [ number , number ]
2120type PaddingType = number | PaddingOptions
2221
2322type 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
5168type 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
0 commit comments