import UIKit
import CoreData
import Foundation
import MapKit
struct Geoshape : Codable {
let coordinates: [[Double]]
}
struct Field : Codable {
let geo_shape: Geoshape
let level_of_service: String
}
struct Record: Codable {
let fields: Field
}
struct Verkeer: Codable{
let records: Array<Record>
}
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
var managedContext:NSManagedObjectContext?
var coordinaten: [[[Double]]] = []
var levels: [String] = []
@IBOutlet weak var myMap: MKMapView!
let initialLocation = CLLocation(latitude: 50.841890, longitude: 4.323475)
override func viewDidLoad() {
super.viewDidLoad()
myMap.delegate = self
centerMapOnLocation(location: initialLocation)
}
@IBAction func clickRefresh(_ sender: Any) {
//Refresh -> array leegmaken en waardes opnieuw toevoegen in core data
coordinaten = []
levels = []
guard let url = URL(string: "https://opendata.brussels.be/api/records/1.0/search/?dataset=traffic-volume&rows=70&facet=level_of_service") else { return }
let session = URLSession.shared
let task = session.dataTask(with: url){ (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
let traffic = try? JSONDecoder().decode(Verkeer.self, from: data)
let test = traffic?.records
for loop in test! {
//Coördinaten
//print(loop.fields.geo_shape.coordinates)
self.coordinaten.append(loop.fields.geo_shape.coordinates)
//Levels
//print(loop.fields.level_of_service)
self.levels.append(loop.fields.level_of_service)
}
var points = [MKMapPoint]()
for x in 0..<self.coordinaten.count{
for y in 0..<self.coordinaten[x].count{
print(self.coordinaten[x][y][0])
let p = MKMapPoint(CLLocationCoordinate2DMake(self.coordinaten[x][y][0], self.coordinaten[x][y][1]))
points.append(p)
}
let polyline = MKPolyline(points: points, count: points.count)
self.myMap.addOverlay(polyline)
points.removeAll()
print(points.count)
}
}
}
task.resume()
}
let regionRadius: CLLocationDistance = 200
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegion(center: location.coordinate,
latitudinalMeters: regionRadius, longitudinalMeters: regionRadius)
myMap.setRegion(coordinateRegion, animated: true)
//Add pin
let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = CLLocationCoordinate2DMake(initialLocation.coordinate.latitude, initialLocation.coordinate.longitude);
pin.title = "Current location"
myMap.addAnnotation(pin)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let lineView = MKPolylineRenderer(overlay: overlay)
lineView.strokeColor = UIColor.red
lineView.lineWidth = 10
return lineView
}
return MKOverlayRenderer()
}
}
因此,我一直在尝试在地图上显示一些坐标,但似乎不起作用。我从json文件中获取坐标,然后打印它们是没有问题的,但是它是我尝试将其添加到地图中的部分。任何人都知道为什么这个“功能映射视图”不是被叫吗?当我在线搜索时,我唯一能找到的就是在viewDidLoad()函数中添加地图委托,但这对我也不起作用。
答案 0 :(得分:0)
这是因为您的叠加层不可见,因为纬度和经度是向后的。交换它们,您将开始看到叠加层:
let coordinate = CLLocationCoordinate2DMake(self.coordinaten[x][y][1], self.coordinaten[x][y][0])