@@ -63,33 +63,126 @@ public class MapViewCoordinator: NSObject {
63
63
}
64
64
65
65
switch camera. state {
66
- case let . centered( onCoordinate: coordinate, zoom: zoom, pitch: pitch, direction: direction) :
66
+ case let . centered(
67
+ onCoordinate: coordinate,
68
+ zoom: zoom,
69
+ pitch: pitch,
70
+ pitchRange: pitchRange,
71
+ direction: direction
72
+ ) :
67
73
mapView. userTrackingMode = . none
68
- mapView. setCenter ( coordinate,
69
- zoomLevel: zoom,
70
- direction: direction,
71
- animated: animated)
72
- mapView. minimumPitch = pitch. rangeValue. lowerBound
73
- mapView. maximumPitch = pitch. rangeValue. upperBound
74
- case let . trackingUserLocation( zoom: zoom, pitch: pitch, direction: direction) :
74
+
75
+ if mapView. frame. size == . zero {
76
+ // On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
77
+ // so let's do something else instead.
78
+ mapView. setCenter ( coordinate,
79
+ zoomLevel: zoom,
80
+ direction: direction,
81
+ animated: animated)
82
+
83
+ // this is a workaround for no camera - minimum and maximum will be reset below, but this adjusts it.
84
+ mapView. minimumPitch = pitch
85
+ mapView. maximumPitch = pitch
86
+
87
+ } else {
88
+ let camera = mapView. camera
89
+ camera. centerCoordinate = coordinate
90
+ camera. heading = direction
91
+ camera. pitch = pitch
92
+
93
+ let altitude = MLNAltitudeForZoomLevel ( zoom, pitch, coordinate. latitude, mapView. frame. size)
94
+ camera. altitude = altitude
95
+ mapView. setCamera ( camera, animated: animated)
96
+ }
97
+
98
+ mapView. minimumPitch = pitchRange. rangeValue. lowerBound
99
+ mapView. maximumPitch = pitchRange. rangeValue. upperBound
100
+ case let . trackingUserLocation( zoom: zoom, pitch: pitch, pitchRange: pitchRange, direction: direction) :
75
101
mapView. userTrackingMode = . follow
76
- // Needs to be non-animated or else it messes up following
77
- mapView. setZoomLevel ( zoom, animated: false )
78
- mapView. direction = direction
79
- mapView. minimumPitch = pitch. rangeValue. lowerBound
80
- mapView. maximumPitch = pitch. rangeValue. upperBound
81
- case let . trackingUserLocationWithHeading( zoom: zoom, pitch: pitch) :
102
+
103
+ if mapView. frame. size == . zero {
104
+ // On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
105
+ // so let's do something else instead.
106
+ // Needs to be non-animated or else it messes up following
107
+
108
+ mapView. setZoomLevel ( zoom, animated: false )
109
+ mapView. direction = direction
110
+
111
+ mapView. minimumPitch = pitch
112
+ mapView. maximumPitch = pitch
113
+
114
+ } else {
115
+ let camera = mapView. camera
116
+ camera. heading = direction
117
+ camera. pitch = pitch
118
+
119
+ let altitude = MLNAltitudeForZoomLevel (
120
+ zoom,
121
+ pitch,
122
+ mapView. camera. centerCoordinate. latitude,
123
+ mapView. frame. size
124
+ )
125
+ camera. altitude = altitude
126
+ mapView. setCamera ( camera, animated: animated)
127
+ }
128
+ mapView. minimumPitch = pitchRange. rangeValue. lowerBound
129
+ mapView. maximumPitch = pitchRange. rangeValue. upperBound
130
+ case let . trackingUserLocationWithHeading( zoom: zoom, pitch: pitch, pitchRange: pitchRange) :
82
131
mapView. userTrackingMode = . followWithHeading
83
- // Needs to be non-animated or else it messes up following
84
- mapView. setZoomLevel ( zoom, animated: false )
85
- mapView. minimumPitch = pitch. rangeValue. lowerBound
86
- mapView. maximumPitch = pitch. rangeValue. upperBound
87
- case let . trackingUserLocationWithCourse( zoom: zoom, pitch: pitch) :
132
+
133
+ if mapView. frame. size == . zero {
134
+ // On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
135
+ // so let's do something else instead.
136
+ // Needs to be non-animated or else it messes up following
137
+
138
+ mapView. setZoomLevel ( zoom, animated: false )
139
+ mapView. minimumPitch = pitch
140
+ mapView. maximumPitch = pitch
141
+
142
+ } else {
143
+ let camera = mapView. camera
144
+
145
+ let altitude = MLNAltitudeForZoomLevel (
146
+ zoom,
147
+ pitch,
148
+ mapView. camera. centerCoordinate. latitude,
149
+ mapView. frame. size
150
+ )
151
+ camera. altitude = altitude
152
+ camera. pitch = pitch
153
+ mapView. setCamera ( camera, animated: animated)
154
+ }
155
+
156
+ mapView. minimumPitch = pitchRange. rangeValue. lowerBound
157
+ mapView. maximumPitch = pitchRange. rangeValue. upperBound
158
+ case let . trackingUserLocationWithCourse( zoom: zoom, pitch: pitch, pitchRange: pitchRange) :
88
159
mapView. userTrackingMode = . followWithCourse
89
- // Needs to be non-animated or else it messes up following
90
- mapView. setZoomLevel ( zoom, animated: false )
91
- mapView. minimumPitch = pitch. rangeValue. lowerBound
92
- mapView. maximumPitch = pitch. rangeValue. upperBound
160
+
161
+ if mapView. frame. size == . zero {
162
+ // On init, the mapView's frame is not set up yet, so manipulation via camera is broken,
163
+ // so let's do something else instead.
164
+ // Needs to be non-animated or else it messes up following
165
+
166
+ mapView. setZoomLevel ( zoom, animated: false )
167
+ mapView. minimumPitch = pitch
168
+ mapView. maximumPitch = pitch
169
+
170
+ } else {
171
+ let camera = mapView. camera
172
+
173
+ let altitude = MLNAltitudeForZoomLevel (
174
+ zoom,
175
+ pitch,
176
+ mapView. camera. centerCoordinate. latitude,
177
+ mapView. frame. size
178
+ )
179
+ camera. altitude = altitude
180
+ camera. pitch = pitch
181
+ mapView. setCamera ( camera, animated: animated)
182
+ }
183
+
184
+ mapView. minimumPitch = pitchRange. rangeValue. lowerBound
185
+ mapView. maximumPitch = pitchRange. rangeValue. upperBound
93
186
case let . rect( boundingBox, padding) :
94
187
mapView. setVisibleCoordinateBounds ( boundingBox,
95
188
edgePadding: padding,
@@ -244,8 +337,8 @@ extension MapViewCoordinator: MLNMapViewDelegate {
244
337
// state propagation.
245
338
let newCamera : MapViewCamera = . center( mapView. centerCoordinate,
246
339
zoom: mapView. zoomLevel,
247
- // TODO: Pitch doesn't really describe current state
248
- pitch : . freeWithinRange(
340
+ pitch : mapView . camera . pitch ,
341
+ pitchRange : . freeWithinRange(
249
342
minimum: mapView. minimumPitch,
250
343
maximum: mapView. maximumPitch
251
344
) ,
0 commit comments