Created
January 18, 2017 10:42
-
-
Save misato/0de9e1ab29bb832a613da764dd5cad45 to your computer and use it in GitHub Desktop.
Mapbox iOS GeoJSON with clustering example (swift)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MapViewController.swift | |
// Mapbox Clustering Test | |
// | |
// Created by Ester Sanchez on 18/01/17. | |
// Copyright © 2017 misato.es. All rights reserved. | |
// | |
import UIKit | |
import Mapbox | |
class MapViewController: UIViewController, MGLMapViewDelegate { | |
@IBOutlet weak var mapView: MGLMapView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
mapView.delegate = self | |
} | |
// Based on this example: https://gist.github.com/boundsj/1a239f5f3e4d27eb7a4fba54db328152 | |
// Updated for Swift 3 and Mapbox iOS SDK 3.4.0.beta 7 (January 2017) | |
private func loadGeoJSON() { | |
let url = URL(string: "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson")! | |
let geoJSONSource = MGLShapeSource(identifier: "earthquakes", url: url, options: [.clustered: true, .clusterRadius: 20, .maximumZoomLevelForClustering: 15]) | |
mapView.style?.addSource(geoJSONSource) | |
let unclusteredLayer = MGLCircleStyleLayer(identifier: "unclustered", source: geoJSONSource) | |
unclusteredLayer.circleColor = MGLStyleValue(rawValue: UIColor(colorLiteralRed: 229/255.0, green: 94/255.0, blue: 94/255.0, alpha: 1)) | |
unclusteredLayer.circleRadius = MGLStyleValue(rawValue: 20) | |
unclusteredLayer.circleBlur = MGLStyleValue(rawValue: 15) | |
unclusteredLayer.predicate = NSPredicate(format: "%K != YES", argumentArray: ["cluster"]) | |
mapView.style?.addLayer(unclusteredLayer) | |
let layers = [ | |
(150, UIColor(colorLiteralRed: 229/255.0, green: 94/255.0, blue: 94/255.0, alpha: 1)), | |
(20, UIColor(colorLiteralRed: 249/255.0, green: 136/255.0, blue: 108/255.0, alpha: 1)), | |
(0, UIColor(colorLiteralRed: 251/255.0, green: 176/255.0, blue: 59/255.0, alpha: 1)) | |
] | |
for index in 0..<layers.count { | |
let circles = MGLCircleStyleLayer(identifier: "cluster-\(index)", source: geoJSONSource) | |
circles.circleColor = MGLStyleValue(rawValue: layers[index].1) | |
circles.circleRadius = MGLStyleValue(rawValue: 70) | |
circles.circleBlur = MGLStyleValue(rawValue: 1) | |
let gtePredicate = NSPredicate(format: "%K >= %@", argumentArray: ["point_count", layers[index].0]) | |
let allPredicate = index == 0 ? | |
gtePredicate : | |
NSCompoundPredicate(andPredicateWithSubpredicates: [gtePredicate, NSPredicate(format: "%K < %@", argumentArray: ["point_count", layers[index - 1].0])]) | |
circles.predicate = allPredicate | |
mapView.style?.addLayer(circles) | |
} | |
} | |
//MARK: - MGLMapViewDelegate | |
func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) { | |
loadGeoJSON() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, could you update it for Swift 4 and iOS 11 ? This would be extremely useful. 🥇