在tableview中根据MKmapview搜索提供建议

时间:2015-11-24 16:10:24

标签: ios uitableview mkmapview uisearchcontroller mklocalsearch

我有一张地图,在地图上我做了一个可行的搜索栏。您可以在其中编写您想要的内容,它会找到该位置并在其中添加注释。问题是我想要显示一个tableview,它会在我写完之后给出结果。因为现在它只是猜测并采取它认为正确的,这是错误的。那么我如何根据我在搜索栏中写的内容显示不同的建议?有人知道吗?我的搜索栏使用的代码低于

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

1 个答案:

答案 0 :(得分:0)

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

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

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