因此,我試圖使用MapKit將一系列點放入地圖中。最後,我將從csv獲取數據點列表,並將它們加載到位置類中,然後將它們從類中讀取爲MapView註釋。這個工作在這裏單獨繪製2點手動...但我不知道如何正確加載和訪問許多項目的位置類(例如10個位置點)快速寫入和訪問類對象
這是我的班級文件
import Foundation
import MapKit
class Location: NSObject, MKAnnotation{
let title: String?
let locationName: String
let discipline: String
let coordinate: CLLocationCoordinate2D
init(title: String, locationName: String, lat: String, lon: String){
self.title = title
self.locationName = locationName
let latDouble = (lat as NSString).doubleValue
let lonDouble = (lon as NSString).doubleValue
let latlong = CLLocationCoordinate2D(latitude: latDouble, longitude: lonDouble)
self.discipline = locationName
self.coordinate = latlong
super.init()
}
}
這裏是ViewController.swift
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
//var stops = TransitStop[]()
let initialLocation = CLLocation(latitude: 41.880632, longitude: -87.623277)
@IBOutlet weak var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
override func viewDidLoad() {
super.viewDidLoad()
let initial = Location(title: "YOU", locationName: "are here", lat: "41.880632", lon: "-87.623277")
let firstStop = Location(title: "ashland", locationName: "ashland", lat: "41.88574", lon: "-87.627835")// Do any additional setup after loading the view, typically from a nib.
centerMapOnLocation(location: initialLocation)
mapView.addAnnotation(initial)
mapView.addAnnotation(firstStop)
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
}
我假設你會從你的csv獲取所有必要的信息,包括lat,long和位置的標題。
將所有項目從csv獲取到模型對象數組,然後循環遍歷元素,並從數組值中初始化您的'Location'對象並將它們添加爲註釋。 –
概念上我明白這是我想要做的。但我想我沒有這樣做的正確語法。你能提供一個基於這個代碼的例子嗎? – Lew