【问题标题】:Giving suggestions based on MKmapview search in a tableview基于 MKmapview 在 tableview 中搜索给出建议
【发布时间】:2015-11-24 16:10:24
【问题描述】:

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

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

【问题讨论】:

    标签: ios uitableview mkmapview uisearchcontroller mklocalsearch


    【解决方案1】:

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

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

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

    【讨论】:

    • 所以我只是将一个新场景添加到带有 tableview 的情节提要中,并用于 localSearchResponse?.mapItems { The table view here }跨度>
    • 正确。或者,如果有意义的话,您可以在同一个 ViewController 上同时添加一个 tableView 和 MapView。
    • 是的。大概。我不想创建一个新视图,只需将其添加到旧视图之上。必须弄清楚如何做到这一点。感谢杰森的帮助。
    • 我得到一个错误,使用该代码与此“可选类型的值'[MKMapItem]?'没有打开;你是不是要使用“!”还是“?”?”
    • 抱歉,是 localSearchResponse!.mapItems 或者您可以通过 guard 或 'if let' 语句打开它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多