Created
June 14, 2020 20:26
-
-
Save mfazekas/4a991dfbd0345c86d4b360596ee178d8 to your computer and use it in GitHub Desktop.
Mapbox
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
import UIKit | |
import Mapbox | |
class ViewController: UIViewController, MGLMapViewDelegate { | |
var mapView : MGLMapView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
createUI() | |
} | |
func createUI() { | |
let url = URL(string: "mapbox://styles/mapbox/streets-v11") | |
mapView = MGLMapView(frame: view.bounds, styleURL: url) | |
mapView.delegate = self | |
mapView.setCenter(CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), zoomLevel: 9, animated: false) | |
self.view.addSubview(mapView) | |
mapView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
mapView.leftAnchor.constraint(equalTo: view.leftAnchor), | |
mapView.rightAnchor.constraint(equalTo: view.rightAnchor), | |
mapView.topAnchor.constraint(equalTo: view.topAnchor), | |
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor) | |
]) | |
let button1 = UIButton() | |
button1.setTitle("up", for: .normal) | |
button1.setTitleColor(.systemBlue, for: .normal) | |
button1.backgroundColor = .systemBackground | |
button1.addTarget(self, action: #selector(up), for: .touchUpInside) | |
let button2 = UIButton() | |
button2.setTitle("down", for: .normal) | |
button2.setTitleColor(.systemBlue, for: .normal) | |
button2.backgroundColor = .systemBackground | |
button2.addTarget(self, action: #selector(down), for: .touchUpInside) | |
let buttons = UIStackView(arrangedSubviews: [button1, button2]) | |
buttons.translatesAutoresizingMaskIntoConstraints = false | |
buttons.spacing = 32.0 | |
self.view.addSubview(buttons) | |
NSLayoutConstraint.activate([ | |
buttons.bottomAnchor.constraint(equalTo: self.view.layoutMarginsGuide.bottomAnchor), | |
buttons.centerXAnchor.constraint(equalTo: self.view.centerXAnchor) | |
]) | |
} | |
func bounds() -> MGLCoordinateBounds { | |
let diff = 0.1 | |
let center = CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06) | |
let sw = CLLocationCoordinate2D(latitude: center.latitude - diff, longitude: center.longitude - diff); | |
let ne = CLLocationCoordinate2D(latitude: center.latitude + diff, longitude: center.longitude + diff); | |
return MGLCoordinateBounds(sw: sw, ne: ne); | |
} | |
func drawBounds(_ style: MGLStyle) { | |
let b = bounds() | |
let coordinates = [ | |
b.sw, | |
CLLocationCoordinate2D(latitude: b.sw.latitude, longitude: b.ne.longitude), | |
b.ne, | |
CLLocationCoordinate2D(latitude: b.ne.latitude, longitude: b.sw.longitude), | |
b.sw | |
] | |
let polyline = MGLPolyline(coordinates: coordinates, count: UInt(coordinates.count)) | |
let source = MGLShapeSource(identifier: "polyline", shape: polyline, options: nil) | |
style.addSource(source) | |
let layer = MGLLineStyleLayer(identifier: "polyline", source: source) | |
layer.lineColor = NSExpression(forConstantValue: UIColor.red) | |
layer.lineWidth = NSExpression(forConstantValue: 4.0) | |
let expression = NSExpression(mglJSONObject:[ | |
"case", | |
["all", true, false], | |
0.5, | |
["all", true, false], | |
0.4, | |
1.1, | |
]); | |
print("expression:",expression, "json", expression.mgl_jsonExpressionObject) | |
layer.lineOpacity = expression | |
style.addLayer(layer) | |
} | |
@objc | |
func up() { | |
let camera = mapView.camera(mapView.camera.copy() as! MGLMapCamera, | |
fitting: bounds(), | |
edgePadding:UIEdgeInsets(top: 200.0, left: 0, bottom: 0.0, right: 0)) | |
mapView.fly(to: camera, withDuration: 1.0, completionHandler: nil) | |
} | |
@objc | |
func down() { | |
let camera = mapView.camera( | |
mapView.camera.copy() as! MGLMapCamera, | |
fitting: bounds(), | |
edgePadding:UIEdgeInsets(top: 0.0, left: 0, bottom: 200.0, right: 0)) | |
mapView.fly(to: camera, withDuration: 1.0, completionHandler: nil) | |
} | |
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle ) { | |
drawBounds(style) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment