mapbox / MapboxGeocoder.swift

Address search and reverse geocoding in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
https://www.mapbox.com/geocoding/
ISC License
131 stars 48 forks source link

Add example using UISearchController to show autocompleted results #137

Open captainbarbosa opened 6 years ago

captainbarbosa commented 6 years ago

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

anoffsinger commented 6 years ago

+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.

1ec5 commented 6 years ago

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

jbarros35 commented 5 years ago

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.

mtcamesao commented 4 years ago

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.

1ec5 commented 4 years ago

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!