[英]How can I compare if I select my location in Swift?
I have created an application that contains a mapView.我创建了一个包含 mapView 的应用程序。 I added a couple of annotations around my location.
我在我的位置周围添加了一些注释。 I want when I click on an annotation to show me the title in a label.
我希望在单击注释时向我显示 label 中的标题。
I did that and I took the title and put it in myLabel but when I'm clicking on my location, the application stops working and I get the error:我这样做了,我把标题放在 myLabel 中,但是当我点击我的位置时,应用程序停止工作,我收到错误:
(Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)
(线程 1:致命错误:在展开可选值时意外发现 nil)
Here is my code:这是我的代码:
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
}
}
}
This error happens just when I click on my location and code is working for other annotations.仅当我单击我的位置并且代码适用于其他注释时,才会发生此错误。
Anyone know how can I fix this?任何人都知道我该如何解决这个问题?
Your selectedAnotation or its subtitle is nil.. thats why you are getting this crash... use optional chaining or optional binding to avoid this.. Roughly something like this... // not tested code您的 selectedAnotation 或其副标题为 nil .. 这就是您遇到此崩溃的原因...使用可选链接或可选绑定来避免这种情况.. 大致如下... // 未测试代码
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)"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.