0

I have a map and on the map ive made a search bar that works. You can write what you want in it and it will find that place and put an annotation in it. The thing is i want to display a tableview that will give me result after what im writing. Because now it will just guess and take what it think is correct which is usally wrong. So how do i display different suggestions based on what im writing in my searchbar? Does anybody know? The code im using for my searchbar is down below

    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
    }
}
Tim
  • 69
  • 10

1 Answers1

0

Just enumerate the MKMapItems in your localSearchResponse and add those to a tableview

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

Currently you are just grabbing the last mapItem in the list and displaying an annotation for it.

  • So instead of self.mapItemData = localSearchResponse?.mapItems.last i just add a new scene to the storyboard with a tableview and use for item in localSearchResponse?.mapItems { The table view here } – Tim Nov 24 '15 at 17:52
  • Correct. Or you could add a tableView and MapView together on the same ViewController if that makes sense. – Jason Cobb Nov 24 '15 at 17:55
  • Yeah. Probably. I dont want to make a new view just add it ontop of the old one. Got to figure out how to do that tho. Thanks for the help Jason. – Tim Nov 24 '15 at 18:04
  • I get an error tho using that code with this "Value of optional type '[MKMapItem]?' not unwrapped; did you mean to use '!' or '?'?" – Tim Nov 24 '15 at 18:06
  • Sorry, yes localSearchResponse!.mapItems or you can unwrap it via guard or 'if let' statements. – Jason Cobb Nov 24 '15 at 18:08
  • Im sorry for having so many question Jason but as u can tell im a new at swift and the world of IOS development. What is the best way to display the tableView here? As a like a popup. And like when u select a adress or the location it closes and use that information to make the annotation. Do you understand what i mean? – Tim Nov 24 '15 at 18:28
  • No problem Tim. I suggest looking at the Apple Maps app on the iPhone for an example on this could look. – Jason Cobb Nov 24 '15 at 18:34