启用自定义属性以更改MGLSymbolStyleLayer的图像 [英] Switching on custom property to change image of MGLSymbolStyleLayer

查看:249
本文介绍了启用自定义属性以更改MGLSymbolStyleLayer的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要分配给引脚的枚举,因此根据该枚举值,引脚图​​像会有所不同:

I have such enum which I'd like to assign to the pins, so that depending on this enum value, the pin image is different:

typedef NS_ENUM(NSInteger, MyType) {
    MyTypeUnknown = 0,
    MyTypeOne,
    MyTypeTwo,
};

我有用于非聚集针脚的层:

I've got my layer for non-clustered pins:

MGLSymbolStyleLayer *markerLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"markerLayerId" source:source];
markerLayer.predicate = [NSPredicate predicateWithFormat:@"cluster != YES"];
[style addLayer: markerLayer];

,我知道我想基于该引脚的type添加各种图像.我唯一能确定的是,我需要将这些图像添加到图层中:

and I know I want to add various images based on type of the pin. The only thing I'm sure about is that I need to add those images to the layer:

[style setImage:[UIImage imageNamed:@"img0"] forName:@"img0id"];
[style setImage:[UIImage imageNamed:@"img1"] forName:@"img1id"];
[style setImage:[UIImage imageNamed:@"img2"] forName:@"img2id"];

现在我应该设置名称,但不确定如何:

Now I should set the name, but I'm not sure how:

markerLayer.iconImageName = [NSExpression expressionForConstantValue:@"???"]; // or withFormat..?

我已经覆盖了他们的课程以添加我的自定义属性:

I've overridden their class to add my custom properties:

@objc class MyPointFeature: MGLPointFeature {
    @objc var type: MyType = .unknown
}

而且我真的迷失了如何打开type属性以设置引脚的图像.有什么帮助吗?

And I'm really lost how to switch on that type property to set the image of the pin. Any help please?

推荐答案

首先,我们需要有一个变量,该变量可用于访问值.为了使其更具可读性,让我们创建一个满足我们需求的变量(例如,在选择销钉时),并立即输入MapBox的需求:

Firstly we need to have a variable, which can be used for accessing the values. To make it most readable, let's create a variable for our needs (later, eg when selecting a pin) and the entry for MapBox needs at once:

@objc class MyPointFeature: MGLPointFeature {
    @objc var type: MyType = .unknown {
        didSet {
            self.attributes = ["myType": MyPointFeature .staticTypeKey(dynamicType: type)]
        }
    }

    // This is made without dynamic property name to string cast, because
    // the values shouldn't be changed so easily and a developer without
    // suspecting it's a key somewhere could accidentally create a bug
   // PS. if you have swift-only code, you can make it in MyType enum
   @objc static func staticTypeKey(dynamicType: MyType) -> String {
        switch dynamicType {
        case .one:
            return "one"
        case .two:
            return "two"
        default:
            return "unknown"
        }
    }
}

现在我们要为给定的键注册图像名称:

Now we want to register image names to given keys:

[style setImage:[UIImage imageNamed:@"img0"] forName:@"img0Key"];
[style setImage:[UIImage imageNamed:@"img1"] forName:@"img1Key"];
[style setImage:[UIImage imageNamed:@"img2"] forName:@"img2Key"];

最后,让我们将图像名称键与先前分配的属性绑定在一起:

Finally, let's bind the image name keys with assigned previously attribute:

NSDictionary *typeIcons = @{[MyPointFeature staticTypeKeyWithDynamicType:MyTypeUnknown]: @"img0Key",
                            [MyPointFeature staticTypeKeyWithDynamicType:MyTypeOne]: @"img1Key",
                            [MyPointFeature staticTypeKeyWithDynamicType:MyTypeTwo]: @"img2Key"};
myLayer.iconImageName = [NSExpression expressionWithFormat:@"FUNCTION(%@, 'valueForKeyPath:', myType)", typeIcons];

显然,键名应该用一些常量等包装,但是重构留给读者,这是最简单的工作解决方案.

Obviously, key names should be wrapped in some constants etc, but refactoring is left for the reader, that's the simplest working solution.

这篇关于启用自定义属性以更改MGLSymbolStyleLayer的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆