import UIKit import Mapbox @objc(MBXOverflowViewController) class OverflowViewController: UIViewController { var mapView: MGLMapView! override func viewDidLoad() { super.viewDidLoad() MGLAccountManager.accessToken = "pk.eyJ1IjoiZnJlZGVyb25pIiwiYSI6ImNqbG1mZ2xkYzEzeTAzcXFyNnp5emJjYWkifQ.DzkRSp9cYtuFLTDvV6_ptQ" mapView = MGLMapView(frame: view.bounds) mapView.delegate = self view.addSubview(mapView) let location = CLLocationCoordinate2D(latitude: 52.412253, longitude: 9.632462) mapView.centerCoordinate = location mapView.zoomLevel = 17 } } extension OverflowViewController: MGLMapViewDelegate { func testCoordinates() -> [CLLocationCoordinate2D] { let coords: [[CLLocationDegrees]] = [[9.6315313, 52.4133574], [9.6313964, 52.4135014], [9.6313843, 52.413515700000005], [9.631266400000001, 52.4136548]] return coords.map { CLLocationCoordinate2D(latitude: $0[1], longitude: $0[0]) } } func testCoordinates2() -> [CLLocationCoordinate2D] { // let coords: [[CLLocationDegrees]] = [[9.6315313, 52.4133574], // [9.6313964, 52.4135014]] let coords: [[CLLocationDegrees]] = [[7, 52], [8, 52]] return coords.map { CLLocationCoordinate2D(latitude: $0[1], longitude: $0[0]) } } func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) { let jsonPath = Bundle.main.path(forResource: "route", ofType: "json") let data = try! Data(contentsOf: URL(fileURLWithPath: jsonPath!)) let allOfflineCoordinates = try! JSONDecoder().decode([Coordinate].self, from: data) .map { CLLocationCoordinate2D(latitude: $0.latitude, longitude: $0.longitude) } let geojsonCoordinates: [[CLLocationDegrees]] = allOfflineCoordinates.map { return [$0.longitude, $0.latitude] } let encoded = try! JSONEncoder().encode(geojsonCoordinates) let string = String(data: encoded, encoding: .utf8) let sstring = NSString(string: string!) print(sstring) let coordinates = testCoordinates2() //mapView.centerCoordinate = coordinates.first! let polyline = MGLPolylineFeature(coordinates: coordinates, count: UInt(coordinates.count)) let source = MGLShapeSource(identifier: "route123123", shapes: [polyline], options: [.lineDistanceMetrics: true]) //let layer = MGLLineStyleLayer(identifier: "route123123", source: source ) //layer.sourceLayerIdentifier = "route" // layer.lineWidth = NSExpression( // format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'exponential', 1.5, %@)", [14: 4, 18: 40]) // layer.lineGradient = NSExpression( // format: "mgl_interpolate:withCurveType:parameters:stops:($lineProgress, 'linear', nil, %@)", [0: UIColor.red, 1: UIColor.blue]) // layer.lineCap = NSExpression(forConstantValue: "round") style.addSource(source) //style.addLayer(layer) } } struct Coordinate: Codable { let latitude: Double let longitude: Double }