MapboxGeocoder.swift icon indicating copy to clipboard operation
MapboxGeocoder.swift copied to clipboard

Add example using UISearchController to show autocompleted results

Open captainbarbosa opened this issue 7 years ago • 5 comments

An example of how to use MapboxGeocoder.swift's autocomplete functionality with a UISearchController would be particularly useful.

captainbarbosa avatar Mar 02 '18 22:03 captainbarbosa

+1 this would be very helpful.

I'm not an experienced iOS developer and want to use Mapbox for a new Swift project. The lack of documentation around this specific interaction is making me fallback to MapKit rather than Mapbox, which has a fair amount of documentation (both official and unofficial) around this.

anoffsinger avatar Mar 05 '18 02:03 anoffsinger

Putting this example in mapbox/ios-sdk-examples#17 would give it more visibility (i.e., on this examples site).

1ec5 avatar Jul 13 '18 17:07 1ec5

Please wherever do it because there is no clear way to do that. For example I have a search for addresses, I already have the country selected but I need to search streets by name and autocomplete.

jbarros35 avatar Oct 15 '19 09:10 jbarros35

Is this feature still not implemented? I already created an app on the Android side and currently converting it to iOS only to find out a key feature such as this isn't really documented and seems to be pushed aside constantly.

mtcamesao avatar Dec 18 '19 19:12 mtcamesao

This library already provides much of the functionality needed to implement an autocompleting search UI; this ticket only tracks adding an example of doing so. Compared to Android, it’s more straightforward on iOS to set up a standard search UI, and you’d benefit from all the UI customization hooks that would be available as a result. The steps look something like this:

  1. Add a UISearchController and UITableViewController to your view controller. Set your view controller as the search controller’s search updater.
  2. Call Geocoder.geocode(_:completionHandler:) in response to UISearchResultsUpdating.updateSearchResults(for:). Here’s an example set of options to use:
    let options = ForwardGeocodeOptions(query: text)
    options.locale = .autoupdatingCurrent
    options.allowedScopes = [.address, .pointOfInterest]
    options.maximumResultCount = 10
    options.focalLocation = location
    
  3. In the completion handler, stash the placemarks in a property of your view controller and reload the table view.
  4. In UITableViewDataSource methods such as tableView(_:cellForRowAt:), configure a UITableViewCell (or a custom subclass) to display details about the placemark.

This Apple example shows how to do it using MapKit, but the types and principles are similar when using MapboxGeocoder.swift. Hope this gives you a decent starting point!

1ec5 avatar Dec 19 '19 02:12 1ec5