Color clustered features based on clustered point counts. (Swift to C#)

时间:2019-05-31 11:26:34

标签: c# swift xamarin.ios mapbox mapbox-ios

I'm developing an app using Xamarin.iOS and Mapbox iOS SDK (Naxam.Mapbox.iOS NuGet package here. And i'm following this example in MapBox iOS SDK website: https://docs.mapbox.com/ios/maps/examples/clustering/.

And i'm having problems converting this part of code to C#:

// Color clustered features based on clustered point counts.
let stops = [
    20: UIColor.lightGray,
    50: UIColor.orange,
    100: UIColor.red,
    200: UIColor.purple
]

And:

circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor.lightGray, stops)

I'm able to set an unique color to the cluster if i do it like this:

circlesLayer.CircleColor = NSExpression.FromConstant(FromObject(UIColor.Green));

But i'm not managing to do it if i follow the example, in it, the Expression format takes 3 Parameters: string, UiColor, Array

But in C# NSExpression.FromConstant, takes at max 2 params: string and a NSObject[], so i created this: (to match the array "stops" in the examle)

NSDictionary[] stops = new NSDictionary[]
{
    new NSDictionary(new NSNumber(0), FromObject(UIColor.Green)),
    new NSDictionary(new NSNumber(20), FromObject(UIColor.Blue)),
    new NSDictionary(new NSNumber(100), FromObject(UIColor.Red))
};

But it no success, what should i be doing instead?

3 个答案:

答案 0 :(得分:1)

它应该是一个nsfloat而不是整数,因此请定义第一个参数:

new NSNumber(0f)

答案 1 :(得分:0)

您可以为此目的使用NSExpression.FromFormat(string format, NSObject [] parameters)

类似这样的东西:

circlesLayer.CircleColor = NSExpression.FromFormat ("mgl_step:from:stops:(point_count, %@, %@)", stops)

stops在字典中的位置在上面。

答案 2 :(得分:0)

尝试一下:

NSDictionary[] stops = new NSDictionary[]
{
    new NSDictionary(new NSNumber(0f), FromObject(UIColor.Green)),
    new NSDictionary(new NSNumber(20f), FromObject(UIColor.Blue)),
    new NSDictionary(new NSNumber(100f), FromObject(UIColor.Red))
};

使用nsfloat可能是一种方法