简体   繁体   中英

How can I compare if I select my location in Swift?

I have created an application that contains a 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.

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:

(Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)

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

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)" 
      }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM