带有群集的Mapbox中的MGLSymbolStyleLayer不显示自定义图像

时间:2018-12-17 10:31:29

标签: ios objective-c mapbox

我尝试了https://www.mapbox.com/ios-sdk/maps/examples/clustering/中的示例,目前所有图标图像都相似。 我可以知道如何在地图中为不同类别(通过使用ports1.text)设置不同的图像吗?

以下是我的示例代码:-

 MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"clusteredPorts" URL:url options:@{ MGLShapeSourceOptionClustered: @(YES),MGLShapeSourceOptionClusterRadius: @(100)
                                                                                                            }];
    [style addSource:source];
        [style setImage:[UIImage imageNamed:@"Map_Food"] forName:@"icon1"];
        [style setImage:[UIImage imageNamed:@"Map_IT"] forName:@"icon2"];

        MGLSymbolStyleLayer *ports1 = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"ports1" source:source];
        ports1.iconImageName = [NSExpression expressionForConstantValue:@"icon1"];
        ports1.text = [NSExpression expressionForKeyPath:@"category_id"];
        ports1.textFontSize = [NSExpression expressionForConstantValue:@(10)];
        ports1.iconAnchor = [NSExpression expressionForConstantValue:@"bottom"];
        ports1.textAnchor = [NSExpression expressionForConstantValue:@"top"];
        ports1.iconColor = [NSExpression expressionForConstantValue:[ThemeDarkBlueColor colorWithAlphaComponent:0.9]];
        ports1.predicate = [NSPredicate predicateWithFormat:@"cluster != YES"];

        [style addLayer:ports1];

    MGLSymbolStyleLayer *ports2 = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"ports2" source:source];
    ports2.iconImageName = [NSExpression expressionForConstantValue:@"icon2"];
    ports2.text = [NSExpression expressionForKeyPath:@"category_id"];
    ports2.textFontSize = [NSExpression expressionForConstantValue:@(10)];
    ports2.iconAnchor = [NSExpression expressionForConstantValue:@"bottom"];
    ports2.textAnchor = [NSExpression expressionForConstantValue:@"top"];
    ports2.iconColor = [NSExpression expressionForConstantValue:[ThemeDarkBlueColor colorWithAlphaComponent:0.9]];
    ports2.predicate = [NSPredicate predicateWithFormat:@"cluster != YES"];
    [style addLayer:ports2];
    NSDictionary *stops = @{ @20:  [UIColor greenColor],
                             @50:  [UIColor yellowColor],
                             @100: [UIColor orangeColor],
                             @200: ThemeRedColor };

    MGLCircleStyleLayer *circlesLayer = [[MGLCircleStyleLayer alloc] initWithIdentifier:@"clusteredPorts" source:source];
    circlesLayer.circleRadius = [NSExpression expressionForConstantValue:@(30)];
    circlesLayer.circleOpacity = [NSExpression expressionForConstantValue:@0.75];
    circlesLayer.circleStrokeColor = [NSExpression expressionForConstantValue:[ThemeWhiteColor colorWithAlphaComponent:0.75]];
    circlesLayer.circleStrokeWidth = [NSExpression expressionForConstantValue:@1];
    circlesLayer.circleColor = [NSExpression expressionWithFormat:@"mgl_step:from:stops:(point_count, %@, %@)",
                                ThemeGrayColor, stops];
    circlesLayer.predicate = [NSPredicate predicateWithFormat:@"cluster == YES"];
    [style addLayer:circlesLayer];


    MGLSymbolStyleLayer *numbersLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"clusteredPortsNumbers" source:source];
    numbersLayer.textColor = [NSExpression expressionForConstantValue:ThemeWhiteColor];
    numbersLayer.textFontSize = [NSExpression expressionForConstantValue:@(20)];
    numbersLayer.iconAllowsOverlap = [NSExpression expressionForConstantValue:@(YES)];
    numbersLayer.text = [NSExpression expressionWithFormat:@"CAST(point_count, 'NSString')"];
    numbersLayer.predicate = [NSPredicate predicateWithFormat:@"cluster == YES"];
    [style addLayer:numbersLayer];

任何使用[NSPredicate predicateWithFormat:更改图像的解决方案吗?

0 个答案:

没有答案