Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created March 27, 2015 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinnormark/50f4439c695f4ecdb77f to your computer and use it in GitHub Desktop.
Save martinnormark/50f4439c695f4ecdb77f to your computer and use it in GitHub Desktop.
Programmatically change map style in MapboxGL native with swift.
// Using SwiftyJSON
var path = NSBundle.mainBundle().pathForResource("satellite-v7-debug", ofType: "json")
var styles = NSData(contentsOfFile: path!)
var jsonStyles = JSON(data: styles!)
let polygonCoordinates = [
[
8.702266216278076,
56.04999222180692
],
[
8.7001633644104,
56.04860211635043
],
[
8.700292110443115,
56.04260970914898
],
[
8.706600666046143,
56.04241793674957
],
[
8.706622123718262,
56.0457977856487
],
[
8.705828189849854,
56.04718799216029
],
[
8.70490550994873,
56.048542196885634
],
[
8.70391845703125,
56.04945296270642
],
[
8.702266216278076,
56.04999222180692
]
]
var json:JSON = [
"type": "geojson",
"data": [
"type": "FeatureCollection",
"features": [
[
"type": "Feature",
"properties": [:],
"geometry": [
"type": "Polygon",
"coordinates": polygonCoordinates
]
]
]
],
"properties": [
"title": "My field",
"marker-symbol": "monument"
]
]
var layer:JSON = [
"id": "polygon_drawn",
"type": "fill",
"source": "polygon",
"source-layer": "polygon layer",
"paint": [
"fill-color": "#ffffcc"
]
]
jsonStyles["sources"].dictionaryObject?.updateValue(json.object, forKey: "polygon")
jsonStyles["layers"].arrayObject?.append(layer.object)
println("polygon \(json.rawString(encoding: NSUTF8StringEncoding, options: NSJSONWritingOptions.allZeros))")
mapView!.setStyleJSON(jsonStyles.rawString(encoding: NSUTF8StringEncoding, options: NSJSONWritingOptions.allZeros))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment