Geosuggest

Geosuggest is a built-in functionality of the maps API for autofilling and checking the names of organizations and addresses in user search queries. When integrated with the MapKit SDK, Geosuggest can generate a list of geo suggestions and make the search query input process easier and more convenient.

Warning

The functionality of Geosuggest is included in the full version of the MapKit SDK.

Executing a geo suggestion query

The process for executing a geo suggestion query is similar to creating a search query, but has a few important differences. To create a query, follow the steps below:

  1. Create a new instance of the MMKSearchManager class or use an existing one.

    let searchManager = MMKSearch.sharedInstance().createSearchManager(with: .combined)
    
  2. Use the MMKSearchManager.createSuggestSession() method to instantiate a Geosuggest session.

    let suggestSession = searchManager.createSuggestSession()
    
  3. Create an instance of the MMKSuggestOptions class to set the geo suggestion query parameters.

    Example of parameter values for limiting the search to organizations:

    let suggestOptions = {
        let options = MMKSuggestOptions()
        options.suggestTypes: .biz
    }()
    
  4. Using MMKSearchSuggestSession.suggest(withText:window:suggestOptions:responseHandler:), create a new geo suggestion query.

    This example searches for available search queries that contain the "ho" prefix, such as "Hotels" and "Hospitals".

    suggestSession.suggest(
        withText: "ho",
        window: MMKBoundingBox(
            southWest: map.visibleRegion.bottomLeft,
            northEast: map.visibleRegion.topRight
        ),
        suggestOptions: suggestOptions,
        responseHandler: handleSuggestSessionResponse
    )
    
  5. When creating the geo suggestion query, use the following mechanism to retrieve the geo suggestion results.

    func handleSuggestSessionResponse(response: MMKSuggestResponse?, error: Error?) {
        if let error {
            // Handle geo suggest error
            return
        }
    
        // Handle geo suggest response
    }
    

    Pass this handler method as an argument whenever you call a method for creating a new geo suggestion query.

Geosuggest session

Geosuggest sessions are represented by the MMKSearchSuggestSession class, which is used to manage queries.

This class provides the following functionality:

Warning

Same as with search sessions, the app must store a reference to the Geosuggest session object. Otherwise, the query is canceled.

Geo suggestion parameters

With MMKSuggestOptions, you can set:

  • suggestTypes: Type of search objects to be selected (organizations, toponyms, or public transport). This parameter is a bitmask, so you can select multiple types.

  • userPosition: Current position of the user. Used to calculate the distance from the user's location to available suggestion options.

Geo suggestion results

To retrieve the results of a geo suggestion query, pass the responseHandler parameter into MMKSearchSuggestSession.suggest(withText:window:suggestOptions:responseHandler:) method. This handler will be called once the suggestion query finishes.

MMKSearchSuggestSessionResponseHandler takes two parameters:

  1. response: If present, says about a successful geo suggestion query completion. The value of type MMKSuggestResponse contains information about the results of the request.

  2. error: If present, says about an error, occurred while a query execution.

Suggestion data

The MMKSuggestItem class provides a wide range of data, the key ones being:

  • type: Suggestion type (toponym, organization, or public transport).
  • searchText: Text to search for if this suggestion is selected.
  • displayText: Text to display in the query input field.
  • uri: Object URI.
  • distance: Distance to the object.
  • action: Type of action to be performed when this option is selected (substitution or instant search).
  • distance: Distance to the search object.
  • center: Position on the map.

Displaying suggestions

The MMKSuggestItem class provides information for easy display of suggestions on the screen:

  • title: Short title of the suggestion.
  • subtitle: Position data and address.

These parameters are of the MMKSpannableString type. This type represents a string where substrings that match the search query text entered by the user are highlighted. These substrings are supposed to be rendered in accent colors.

Processing suggestion clicks

With MMKSuggestItem.action, you can get the type of action to be performed when the suggestion is selected:

  • search: Perform a new search query. If MMKSuggestItem.uri returns a non-null URI, a URI search is to be performed. If it's missing, search by the text from the MMKSuggestItem.searchText method. This type is usually used when the user clicks a category name, such as "Cafe" or "Restaurant", or the name of an organization.

  • substitute: Replace the current search query text with the text obtained from MMKSuggestItem.displayText and execute a new geo suggestion query. This is typically used when searching for addresses, where the API first substitutes the name of the city, then the street, and so on.