我已经创建了一个包含mapView的应用程序。我在我的位置周围添加了几个注解。我希望当我点击一个注解时,能在一个标签中显示标题。
我这样做了,我把标题放在myLabel中,但是当我点击我的位置时,应用程序停止工作,我得到了错误。
(Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)
这是我的代码。
import UIKit
import MapKit
import CoreLocation
import Foundation
class ViewControllermap: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mymap: MKMapView!
@IBOutlet weak var viewaddress: UILabel!
var selectedAnnotation: MKPointAnnotation?
var locationManager: CLLocationManager = CLLocationManager()
let regionInMeters:Double = 3000
var previouslocation: CLLocation?
var longitudeLocation:String = ""
var latituseLocation:String = ""
override func viewDidLoad() {
mymap.delegate = self
// checkLocationSevices()
super.viewDidLoad()
if let location = locationManager.location?.coordinate
{
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mymap.setRegion(region, animated: true)
}
mymap.showsUserLocation = true
let location = CLLocationCoordinate2DMake(35.1623, 33.3178)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "P1"
annotation.subtitle = "28 oct street"
mymap.addAnnotation(annotation)
let location2 = CLLocationCoordinate2DMake(35.1658, 33.3147)
let annotation2 = MKPointAnnotation()
annotation2.coordinate = location2
annotation2.title = "P2"
annotation2.subtitle = " Makedonitissis 46, Nicosia 2417"
mymap.addAnnotation(annotation2)
let location3 = CLLocationCoordinate2DMake(35.1600, 33.3770)
let annotation3 = MKPointAnnotation()
annotation3.coordinate = location3
annotation3.title = "P3"
annotation3.subtitle = " Kallipoleos 75, Nicosia 1678"
mymap.addAnnotation(annotation3)
let location4 = CLLocationCoordinate2DMake(35.1602, 33.3390)
let annotation4 = MKPointAnnotation()
annotation4.coordinate = location4
annotation4.title = "P4"
annotation4.subtitle = "Diogenis Str 6 Nicosia CY, 2404"
mymap.addAnnotation(annotation4)
let location5 = CLLocationCoordinate2DMake(35.1673, 33.3277)
let annotation5 = MKPointAnnotation()
annotation5.coordinate = location5
annotation5.title = "P5"
annotation5.subtitle = "Neas Egkomis 4, Egkomi"
mymap.addAnnotation(annotation5)
let location6 = CLLocationCoordinate2DMake(35.1680, 33.3375)
let annotation6 = MKPointAnnotation()
annotation6.coordinate = location6
annotation6.title = "P6"
annotation6.subtitle = "Neas Egkomis 4, Egkomi"
mymap.addAnnotation(annotation6)
let location7 = CLLocationCoordinate2DMake(35.1460, 33.3357)
let annotation7 = MKPointAnnotation()
annotation7.coordinate = location7
annotation7.title = "P7"
annotation7.subtitle = "Neas Egkomis 4, Egkomi"
mymap.addAnnotation(annotation7)
let location8 = CLLocationCoordinate2DMake(35.1580, 33.3275)
let annotation8 = MKPointAnnotation()
annotation8.coordinate = location8
annotation8.title = "P8"
annotation8.subtitle = "Neas Egkomis 4, Egkomi"
mymap.addAnnotation(annotation8)
// Do any additional setup after loading the view.
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotationCoordinate = view.annotation?.coordinate {
self.selectedAnnotation = view.annotation as? MKPointAnnotation
viewaddress.text = "\(selectedAnnotation!.subtitle!)" as String
}
}
}
这个错误只是在我点击我的位置时发生,而其他注释的代码也在工作。
有谁知道我如何解决这个问题?
你的selectedAnotation或它的副标题是零......这就是为什么你得到这个崩溃......使用可选的链或可选的绑定,以避免这种情况。/未测试的代码
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotationCoordinate = view.annotation?.coordinate {
self.selectedAnnotation = view.annotation as? MKPointAnnotation
if let annnotation = selectedAnnotation , let subTitle = annnotation.subTitle as? String {
viewaddress.text = "\(subTitle)"
}
}
}