We needed to display the numbers on our map based on a chosen locale. I found the "number-format" style specifications on the Mapbox documentation. Since I can't seem to find a custom function for number formatting, the approach was to use MGL_FUNCTION using "number-format". So I've been on this problem for more than a day now.
Here are the once I have tried
let locale: [String: Any] = ["locale": "en", "min-fraction-digits": 1, "max-fraction-digits": 1]
let jsonData = try? JSONSerialization.data(withJSONObject: locale, options: [])
let jsonString = String(data: jsonData!, encoding: .utf8)!
// Use json string
let formattedNumber = NSExpression(format: "MGL_FUNCTION('number-format', CAST(value, 'NSNumber'), %@)", jsonString)
// Use dictionary
let formattedNumber = NSExpression(format: "MGL_FUNCTION('number-format', CAST(value, 'NSNumber'), %@)", locale)
// Use style specs "object" json string
let object = NSExpression(mglJSONObject: ["object", jsonString])
let formattedNumber = NSExpression(format: "MGL_FUNCTION('number-format', CAST(value, 'NSNumber'), %@)", object)
// Use style specs "object" dictionary
let object = NSExpression(mglJSONObject: ["object", locale])
let formattedNumber = NSExpression(format: "MGL_FUNCTION('number-format', CAST(value, 'NSNumber'), %@)", object)
All of which returns 'Invalid property value: [1][2]: Number-format options argument must be an object.'
Hope anyone would be able to point out what I am doing wrong and the correct syntax/object format to pass for the "number-format" style specifications.
Thanks!
number-format
support was added to the cross-platform style parsing code in 2019, which means this expression operator would work when used inside a style JSON file bundled with the application or hosted on mapbox.com. However, number-format
doesn’t seem to have ever been added to the iOS/macOS map SDK’s NSExpression conversion code. In the runtime styling API on iOS/macOS before v10.0.0, expressions are always round-tripped through NSExpression, even when you use +[NSExpression expressionWithMGLJSONObject:]
.
In general, unrecognized operators should work when you use either +[NSExpression expressionWithMGLJSONObject:]
or MGL_FUNCTION
in an NSExpression format string. However, the NSExpression conversion code wraps any unrecognized JSON object in a literal
expression, which would be invalid for number-format
. There would need to be a special case similar to the one for collator
expressions when converting to NSExpression and when converting to JSON.
A more elegant fix would also include support for embedding an NSNumberFormatter into an NSExpression, but it would be more involved to convert between NSNumberFormatter options and the options accepted by the number-format
operator in the style specification.
The immediate fix to enable MGL_FUNCTION(@"number-format", …)
would be pretty small. You could open an issue in the mapbox-gl-native-ios repository about it, though Mapbox’s priority these days is on the rewrite in mapbox-maps-ios that will be released as v10. In v10, the runtime styling API has been overhauled to align more closely to the style specification; it doesn’t use NSExpression at all.