MKOverlayRenderer 函数未被调用?
MKOverlayRenderer function not being called?
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 文件中获取坐标并打印它们没有问题,但它是我尝试将它们添加到我的地图的部分。任何人都知道为什么这个 "func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer" 没有被调用?当我在网上搜索时,我唯一能找到的是在 viewDidLoad() 函数中添加地图委托,但这对我也不起作用..
这是因为您的叠加层不可见,因为纬度和经度是向后的。交换它们,您将开始看到您的叠加层:
let coordinate = CLLocationCoordinate2DMake(self.coordinaten[x][y][1], self.coordinaten[x][y][0])
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 文件中获取坐标并打印它们没有问题,但它是我尝试将它们添加到我的地图的部分。任何人都知道为什么这个 "func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer" 没有被调用?当我在网上搜索时,我唯一能找到的是在 viewDidLoad() 函数中添加地图委托,但这对我也不起作用..
这是因为您的叠加层不可见,因为纬度和经度是向后的。交换它们,您将开始看到您的叠加层:
let coordinate = CLLocationCoordinate2DMake(self.coordinaten[x][y][1], self.coordinaten[x][y][0])