@@ -148,13 +148,25 @@ - (void)update {
148148 }
149149
150150 GMSMutablePath *path = [GMSMutablePath path ];
151- for (CLLocation *location in self.coordinates ) {
152- [path addCoordinate: location.coordinate];
151+ CLLocationCoordinate2D lastCoord = self.coordinates .firstObject .coordinate ;
152+ [path addCoordinate: lastCoord];
153+ for (NSUInteger i = 1 ; i < self.coordinates .count ; i++) {
154+ CLLocationCoordinate2D coord = self.coordinates [i].coordinate ;
155+ if (coord.latitude == lastCoord.latitude && coord.longitude == lastCoord.longitude ) {
156+ continue ;
157+ }
158+ [path addCoordinate: coord];
159+ lastCoord = coord;
153160 }
161+
162+ // Clear stale spans before swapping the path so Google Maps never rebuilds
163+ // span models against a path with fewer segments (GMSModelStyleBuilder
164+ // couldNotInstantiate -> EXC_BAD_ACCESS). Reassign spans once the new path is set.
165+ _polyline.spans = nil ;
154166 _polyline.path = path;
155167
156- if (self.strokeColors .count > 1 ) {
157- _polyline.spans = [self createGradientSpans ];
168+ if (path. count > 1 && self.strokeColors .count > 1 ) {
169+ _polyline.spans = [self createGradientSpansForSegmentCount: path.count - 1 ];
158170 } else {
159171 _polyline.strokeColor = self.strokeColors .firstObject ?: [UIColor blackColor ];
160172 }
@@ -222,35 +234,44 @@ - (void)updateAnimatedPolyline {
222234 }
223235
224236 if (headDist <= tailDist) {
237+ _polyline.spans = nil ;
225238 _polyline.path = [GMSMutablePath path ];
226239 return ;
227240 }
228241
229- CGFloat visibleLength = headDist - tailDist;
230242 NSUInteger startIndex = [self indexForDistance: tailDist];
231243 NSUInteger endIndex = [self indexForDistance: headDist];
232244
233245 GMSMutablePath *path = [GMSMutablePath path ];
234- NSMutableArray <GMSStyleSpan *> *spans = [NSMutableArray array ];
235246
236- CLLocationCoordinate2D startCoord = [self coordinateAtDistance: tailDist];
237- [path addCoordinate: startCoord ];
247+ CLLocationCoordinate2D lastCoord = [self coordinateAtDistance: tailDist];
248+ [path addCoordinate: lastCoord ];
238249
239250 for (NSUInteger i = startIndex + 1 ; i <= endIndex; i++) {
240- [path addCoordinate: self .coordinates[i].coordinate];
251+ CLLocationCoordinate2D coord = self.coordinates [i].coordinate ;
252+ if (coord.latitude == lastCoord.latitude && coord.longitude == lastCoord.longitude ) {
253+ continue ;
254+ }
255+ [path addCoordinate: coord];
256+ lastCoord = coord;
241257 }
242258
243259 CLLocationCoordinate2D endCoord = [self coordinateAtDistance: headDist];
244- CLLocationCoordinate2D lastAdded =
245- (endIndex < self.coordinates .count ) ? self.coordinates [endIndex].coordinate : endCoord;
246- if (endCoord.latitude != lastAdded.latitude || endCoord.longitude != lastAdded.longitude ) {
260+ if (endCoord.latitude != lastCoord.latitude || endCoord.longitude != lastCoord.longitude ) {
247261 [path addCoordinate: endCoord];
248262 }
249263
250264 NSUInteger pathCount = path.count ;
251- NSUInteger segmentCount = pathCount - 1 ;
265+ NSUInteger segmentCount = (pathCount > 1 ) ? pathCount - 1 : 0 ;
266+
267+ if (segmentCount == 0 ) {
268+ _polyline.spans = nil ;
269+ _polyline.path = path;
270+ return ;
271+ }
252272
253273 if (self.strokeColors .count <= 1 ) {
274+ _polyline.spans = nil ;
254275 _polyline.path = path;
255276 _polyline.strokeColor = self.strokeColors .firstObject ?: [UIColor blackColor ];
256277 return ;
@@ -259,20 +280,24 @@ - (void)updateAnimatedPolyline {
259280 NSUInteger spanCount = MIN (segmentCount, kMaxAnimationSpans );
260281 double segmentsPerSpan = (double )segmentCount / spanCount;
261282
283+ NSMutableArray <GMSStyleSpan *> *spans = [NSMutableArray array ];
262284 for (NSUInteger i = 0 ; i < spanCount; i++) {
263285 CGFloat gradientPos = ((CGFloat)i + 0.5 ) / spanCount;
264286 UIColor *color = [self colorAtGradientPosition: gradientPos];
265287 GMSStrokeStyle *style = [GMSStrokeStyle solidColor: color];
266288 [spans addObject: [GMSStyleSpan spanWithStyle: style segments: segmentsPerSpan]];
267289 }
268290
291+ // Clear stale spans before swapping the path so Google Maps never rebuilds
292+ // span models against a path with fewer segments (GMSModelStyleBuilder
293+ // couldNotInstantiate -> EXC_BAD_ACCESS). Reassign spans once the new path is set.
294+ _polyline.spans = nil ;
269295 _polyline.path = path;
270296 _polyline.spans = spans;
271297}
272298
273- - (NSArray <GMSStyleSpan *> *)createGradientSpans {
299+ - (NSArray <GMSStyleSpan *> *)createGradientSpansForSegmentCount : ( NSUInteger ) segmentCount {
274300 NSMutableArray <GMSStyleSpan *> *spans = [NSMutableArray array ];
275- NSUInteger segmentCount = self.coordinates .count - 1 ;
276301 NSUInteger spanCount = MIN (segmentCount, kMaxGradientSpans );
277302 double segmentsPerSpan = (double )segmentCount / spanCount;
278303
0 commit comments