1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
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
}
|