SHARE
TWEET

Untitled

a guest Aug 19th, 2019 78 in 332 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  Piste
  4. //
  5. //  Created by Byron Coetsee on 2019/02/17.
  6. //  Copyright © 2019 Byron Coetsee. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Mapbox
  11. import SwiftyJSON
  12. import SwiftTurf
  13.  
  14. class ViewController: UIViewController, MGLMapViewDelegate {
  15.    
  16.     @IBOutlet weak var mapView: MGLMapView!
  17.    
  18.     var timer: Timer?
  19.     var polylineSource: MGLShapeSource!
  20.     var shapes: MGLShape!
  21.     var currentIndex = 1
  22.    
  23.     override func viewDidLoad() {
  24.         super.viewDidLoad()
  25.        
  26.         mapView.delegate = self
  27.         mapView.setCenter(
  28.             CLLocationCoordinate2D(latitude: 45.3809819, longitude: 6.6101733),
  29.             zoomLevel: 16,
  30.             animated: false)
  31.        
  32.         let newCameraPosition = MGLMapCamera(lookingAtCenter: mapView.centerCoordinate, acrossDistance: mapView.camera.altitude, pitch: 100, heading: 0)
  33.         mapView.camera = newCameraPosition
  34.     }
  35.    
  36.     func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
  37.         addPolyline(to: mapView.style!)
  38.         mapView.setCenter((shapes as! MGLShapeCollectionFeature).shapes.last!.coordinate, animated: false)
  39.        
  40.         drawPolyline(animated: false)
  41.     }
  42.  
  43.     func addPolyline(to style: MGLStyle) {
  44.         let source = MGLShapeSource(identifier: "polyline", shape: nil, options: nil)
  45.         style.addSource(source)
  46.         polylineSource = source
  47.        
  48.         var maxSpeed = 0.0
  49.         var minAlt = 99999999.0
  50.         var maxAlt = 0.0
  51.        
  52.         for shape in (getGeoJsonShapes() as! MGLShapeCollectionFeature).shapes {
  53.             if let speed = shape.attributes["spd_3d"] as? Double {
  54.                 maxSpeed = speed > maxSpeed ? speed : maxSpeed
  55.             }
  56.            
  57.             if let altitude = shape.attributes["alt"] as? Double {
  58.                 minAlt = altitude < minAlt ? altitude : minAlt
  59.                 maxAlt = altitude > maxAlt ? altitude : maxAlt
  60.             }
  61.         }
  62.        
  63.         print(maxSpeed)
  64.         print(maxAlt)
  65.        
  66.         let magnitudeDictionary: [NSNumber : UIColor] = maxSpeed == 0 ? [0: .blue] : [
  67.             0: .green,
  68.             NSNumber(value: maxSpeed*1/3): .yellow,
  69.             NSNumber(value: maxSpeed*2/3): .orange,
  70.             NSNumber(value: maxSpeed*3/3): .red
  71.         ]
  72.  
  73.         let layer = MGLFillExtrusionStyleLayer(identifier: "polyline", source: source)
  74.         layer.fillExtrusionHasVerticalGradient = NSExpression(forConstantValue: true)
  75.         layer.fillExtrusionHeight = NSExpression(format: "(alt-\(minAlt))/4")
  76.         layer.fillExtrusionOpacity = NSExpression(forConstantValue: 1)
  77.         layer.fillExtrusionColor = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:(spd_3d, 'linear', nil, %@)", magnitudeDictionary)
  78.  
  79.         style.addLayer(layer)
  80.     }
  81.    
  82.     func getGeoJsonShapes() -> MGLShape {
  83.         let jsonPath = Bundle.main.path(forResource: "vidGeoJson", ofType: "geojson")
  84.         let url = URL(fileURLWithPath: jsonPath!)
  85.         let data = try! Data(contentsOf: url)
  86.        
  87.         let shape = try! (MGLShape(data: data, encoding: String.Encoding.utf8.rawValue))
  88.         self.shapes = shape
  89.         return shape
  90.     }
  91.    
  92.    
  93.    
  94.    
  95.    
  96.    
  97.    
  98.    
  99.    
  100.    
  101.    
  102.     func drawPolyline(animated: Bool) {
  103.         if animated {
  104.             currentIndex = 1
  105.             timer = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
  106.         } else {
  107.             polylineSource.shape = shapes
  108.         }
  109.     }
  110.  
  111.     @objc func tick() {
  112.         let shapeCollection = (shapes as! MGLShapeCollectionFeature).shapes
  113.        
  114.         if currentIndex > shapeCollection.count {
  115.             timer?.invalidate()
  116.             return
  117.         }
  118.  
  119.         let newShapes = Array(shapeCollection[0..<currentIndex])
  120.         updateLineWithPoints(newShapeCollection: MGLShapeCollectionFeature(shapes: newShapes))
  121.         currentIndex += 1
  122.     }
  123.  
  124.     func updateLineWithPoints(newShapeCollection: MGLShapeCollectionFeature) {
  125. //        mapView.setCenter(newShapeCollection.shapes.last!.coordinate, animated: true)
  126.         polylineSource.shape = newShapeCollection
  127.     }
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top