我的這個應用程序的目的是允許用戶在一個選項卡控制器的一個選項卡中輸入任何一組座標,而在另一個選項卡中,座標將被放置在一個引腳帶註釋。Swift將座標添加到數組並添加到地圖
我一直在試圖實現的方法是使用全局數組,附加輸入座標並在其他類文件(使用mapView)中調用。在嘗試遍歷數組來放置引腳時,我遇到了一系列的問題,我不確定哪裏出了問題。
輸入信息類文件:
import UIKit
import CoreLocation
var locations: [Dictionary<String, Any>] = [] // here I initialize my array
class OtherVC: UIViewController {
@IBOutlet weak var latitudeField: UITextField!
@IBOutlet weak var longitudeField: UITextField!
@IBOutlet weak var titleTextField: UITextField!
var coordinates = [CLLocationCoordinate2D]()
override func viewDidLoad() {
super.viewDidLoad()
}
// This IBOutlet takes the coordinate input information from the user
@IBAction func addToMap(_ sender: Any) {
let lat = latitudeField.text!
let long = longitudeField.text!
let title = titleTextField.text!
var location: [String: Any] = ["title": title, "latitude": lat, "longitude": long]
locations.append(location) // adding the info to the array
let mapVC : MapViewController = MapViewController()
mapVC.iterateLocations()
}
}
的MapView類文件:
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
// this method here iterates through each of the locations in the array
func iterateLocations() -> Bool {
for location in locations {
var momentaryLat = (location["latitude"] as! NSString).doubleValue
var momentaryLong = (location["longitude"] as! NSString).doubleValue
let annotation = MKPointAnnotation()
annotation.title = location["title"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
mapView.addAnnotation(annotation)
}
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pinAnnotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}
annotationView?.annotation = annotation
return annotationView
}
}
錯誤在該行彈出 「mapView.addAnnotation(註釋)」 它說,一個無發現當試圖解開一個可選。所以我認爲錯誤在於信息沒有保存在註釋中,但我不能立即看到這是錯誤的。
此方法的任何替代方法都可以更容易實現,歡迎您!
啊我現在看到「let mapVC:MapViewController = MapViewController()」並沒有引用由UITabBarViewController控制的實際MapViewController。另外,感謝您指出每次函數迭代時,它都會重複相同的註釋。但是,在使用您的建議之後,MapView上沒有註釋。 (但該應用程序不會崩潰!)你知道可能是什麼問題? – Kevin
我建議嘗試以下方法:1)在將地圖添加到地圖後,將地圖居中放置到註釋的座標上,即''''self.mapView.centerCoordinate = annotation.coordinate'''' 'add'''方法。 2)嘗試一個iOS 9模擬器。 3)添加註釋後,嘗試在調試器中打印mapView.annotations屬性。 4)嘗試UI調試器來查看註釋是否超出範圍。讓我知道是否有任何上述幫助! – stakri
事實證明,西方座標需要輸入爲負座標! self.mapView.centerCoordinate = annotation.coordinate幫我弄明白了! – Kevin