2
votes

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

3 Answers

1
votes

It should be an nsfloat rather than a integer, so define the first param like this:

new NSNumber(0f)
0
votes

You can use NSExpression.FromFormat(string format, NSObject [] parameters) for this purpose.

Something like this:

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

Where stops is what you had above with the dictionary.

0
votes

Try this:

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))
};

Using nsfloat might be the way to go