0

我有一张地图,在地图上我做了一个可以工作的搜索栏。您可以在其中写下您想要的内容,它会找到该位置并在其中添加注释。问题是我想显示一个表格视图,它会在我写完之后给我结果。因为现在它只会猜测并接受它认为正确的东西,而这通常是错误的。那么我如何根据我在搜索栏中写的内容显示不同的建议?有人知道吗?我用于我的搜索栏的代码在下面

    func searchBarSearchButtonClicked(searchBar: UISearchBar){
    //1
    searchBar.resignFirstResponder()
    dismissViewControllerAnimated(true, completion: nil)
    if self.mapView.annotations.count != 0{
        annotation = self.mapView.annotations[0]
        self.mapView.removeAnnotation(annotation)
    }
    //2
    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = searchBar.text
    localSearch = MKLocalSearch(request: localSearchRequest)
    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

        if localSearchResponse == nil{
            let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alertController, animated: true, completion: nil)
            return
        }
        //3
        self.pointAnnotation = MKPointAnnotation()
        self.pointAnnotation.title = searchBar.text
        self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)


        self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
        self.mapView.centerCoordinate = self.pointAnnotation.coordinate
        self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
        self.mapItemData = localSearchResponse?.mapItems.last
    }
}
4

1 回答 1

0

只需枚举 localSearchResponse 中的 MKMapItems 并将它们添加到 tableview

for item in localSearchResponse!.mapItems {
    // add to a tableView
}

目前,您只是抓取列表中的最后一个 mapItem 并为其显示注释。

于 2015-11-24T17:06:02.710 回答