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()
}
}
So I've been trying to show some coordinates on my map, but it does not seem to work. I get the coordinates from a json file and printing them is no problem but its the part where I try to add them to my map.. Anyone knows why this "func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer" is not being called? When I was searching online, the only thing I could find was to add the map delegate in viewDidLoad() function but that also does not work for me..
It’s because your overlay is not visible because the latitude and longitude are backwards. Swap them and you’ll start to see your overlays:
let coordinate = CLLocationCoordinate2DMake(self.coordinaten[x][y][1], self.coordinaten[x][y][0])