在Mapview Mapbox iOS上对折线进行动画处理

时间:2019-07-17 02:34:10

标签: ios objective-c mapbox polyline mapbox-ios

我要用GoogleMaps SDK替换iOS应用中的Mapbox SDK。 我陷入了我的应用程序具有在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;
}

更新: 这是我期望做的礼物。

enter image description here

请帮助我如何为折线上的路线设置动画?

谢谢

1 个答案:

答案 0 :(得分:0)

感谢您以有益的视觉效果更新您的问题。您应该能够使用在计时器上更新的line-gradient样式表达式来创建此效果。

虽然没有完善的示例实现此方法,但Mapbox iOS团队有一些初始代码,在此示例PR中显示了线梯度表达式的基本语法:https://github.com/mapbox/ios-sdk-examples/issues/203。 (定义渐变的特定行是here。)

然后,您可以在计时器上更新MGLLineStyleLayer的{​​{1}}属性的表达式。


⚠️免责声明:我目前在Mapbox⚠️

工作