我正在替换GoogleMaps SDK
我Mapbox SDK
的 iOS 应用程序。我被困在我的应用程序具有在地图上为折线(已添加)制作动画的功能,例如 Uber 应用程序。
但我似乎无法让它与Mapbox iOS SDK
. Mapbox 有一个示例,我可以在地图上添加带有动画的折线坐标,但这不是我想做的。
立即添加折线,但我想为从起点到终点的路线设置动画。
这是我当前来自 Mapbox 示例的代码,用于在带有动画的地图上添加折线坐标。
- (void)addPolylineToStyle:(MGLStyle *)style {
// Add an empty MGLShapeSource, we’ll keep a reference to this and add points to this later.
MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"polyline" features:@[] options:nil];
[style addSource:source];
self.polylineSource = source;
// Add a layer to style our polyline.
MGLLineStyleLayer *layer = [[MGLLineStyleLayer alloc] initWithIdentifier:@"polyline" source:source];
layer.lineJoin = [NSExpression expressionForConstantValue:@"round"];
layer.lineCap = layer.lineJoin = [NSExpression expressionForConstantValue:@"round"];
layer.lineColor = [NSExpression expressionForConstantValue:[UIColor redColor]];
// The line width should gradually increase based on the zoom level.
layer.lineWidth = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
@{@14: @5, @18: @20}];
[self.mapView.style addLayer:layer];
}
- (void)animatePolyline {
self.currentIndex = 1;
// Start a timer that will simulate adding points to our polyline. This could also represent coordinates being added to our polyline from another source, such as a CLLocationManagerDelegate.
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
- (void)tick:(NSTimer*)timer {
if (self.currentIndex > self.locations.count) {
[timer invalidate];
return;
}
// Create a subarray of locations up to the current index.
NSArray *currentLocations = [self.locations subarrayWithRange:NSMakeRange(0, _currentIndex)];
// Update our MGLShapeSource with the current locations.
[self updatePolylineWithLocations:currentLocations];
self.currentIndex++;
}
- (void)updatePolylineWithLocations:(NSArray<CLLocation *> *)locations {
CLLocationCoordinate2D coordinates[locations.count];
for (NSUInteger i = 0; i < locations.count; i++) {
coordinates[i] = locations[i].coordinate;
}
MGLPolylineFeature *polyline = [MGLPolylineFeature polylineWithCoordinates:coordinates count:locations.count];
// Updating the MGLShapeSource’s shape will have the map redraw our polyline with the current coordinates.
self.polylineSource.shape = polyline;
}
更新: 这是我期望做的 gif。
请帮助我如何为折线的路线设置动画?
谢谢