首页 > 解决方案 > 设置注释时为零。mapitems 的价值,mapkit swift 4

问题描述

我正在使用 Xcode 9 和 mapkit 在 swift 4 中创建一个应用程序。现在,我希望用户单击某些按钮,然后在地图上弹出与单击的按钮相对应的注释。示例:单击“餐厅”按钮,以便在给定区域的所有餐厅弹出注释。

将注释添加到我的地图视图时会出现问题。我收到一个致命错误:展开时发现 nil。这是代码:

enum annotations : String {
case restaurants = "Restaurants"
case movieTheater = "Movie Theater"
case shopping = "Shopping"
case bowling = "Bowling"
case park = "Park"
}

//In viewcontroller.swift
var annotationPoints : [MKMapItem] = []

@IBAction func annotationClicked(_ sender: UIButton) {
    guard let title = sender.currentTitle, let annotation =       annotations(rawValue: title) else {
        return
    }
    //only added two cases to test
    switch annotation {
    case .restaurants:
        getAnnotations(query: annotation.rawValue)
    default:
        getAnnotations(query: annotation.rawValue)
    }
}

func getAnnotations(query : String) {
    print("creating request")

    let request  = MKLocalSearchRequest()
    print("setting request")
    request.naturalLanguageQuery = query

    let span = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
    let region = MKCoordinateRegion(center: (mapView.userLocation.location?.coordinate)!, span: span)
    print("setting region")
    request.region = region
    print("creating search")
    let search = MKLocalSearch(request: request)
    print("starting search")
    search.start { (response, error) in
        guard let response = response else {
            return
        }
        self.annotationPoints = response.mapItems
        var annotation : GetAnnotationPins?

        print(self.annotationPoints)
        for points in self.annotationPoints {
            print("the points name is \(String(describing: points.name))")
            print("the points number  is \(String(describing: points.phoneNumber))")
            print("the points coordinates are \(points.placemark.coordinate)")

            annotation?.title = points.name
            print("the title is \(String(describing: annotation?.title))")

            annotation?.subtitle = points.phoneNumber
            print("the subtitle is \(String(describing: annotation?.subtitle))")

            annotation?.coordinate = points.placemark.coordinate
            print("the coordinates are \(String(describing: annotation?.coordinate))")

            print("the annotaiton is \(String(describing: annotation))")


            self.mapView.addAnnotation(annotation!)

        }



//separate class for pin annotation 

class GetAnnotationPins : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var subtitle: String?
var title: String?

init(coordinate : CLLocationCoordinate2D, subtitle : String, title : String) {
    self.coordinate = coordinate
    self.subtitle = subtitle
    self.title = title
}

}

//console print out
the points name is Optional("Lin\'s Garden") // restaurant near my location 
the points number  is Optional("‭+1 (715) 693-8899‬")
the points coordinates are CLLocationCoordinate2D(latitude:        44.791603000000002, longitude: -89.700630000000004)
the title is nil
the subtitle is nil
the coordinates are nil
the annotaiton is nil

标签: swiftmapkit

解决方案


你应该初始化GetAnnotationPins类。试试下面的代码:

for points in self.annotationPoints {
    var annotation = GetAnnotationPins(coordinate: points.placemark.coordinate,
        subtitle: points.phoneNumber,
        title: points.name)
    ....
}

并更换

 self.mapView.addAnnotation(annotation!)

 self.mapView.addAnnotation(annotation)

推荐阅读