Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapKit Define the desired type of search results (Country, city, region, etc)

For an app i'm building, I want to implement a feature that allows users to specify the geographical origin of wines (country (e.g. France), region (e.g. Bordeaux), subregion (e.g. Paullac)).

I want to make sure that I don't have to add all available countries myself, and that all information that comes into the database is valid. Therefore, I decided to do it as follows:

  1. User adds a new wine and types the name of the country it comes from
  2. While typing, the app searches in the apple maps database
  3. The results from this search get displayed as suggestions, and when the user taps a suggestion, the app creates a Country object with all relevant information. The wine van only be saved when such an object is present

This works fine, except one thing: Apple maps returns anything, like restaurants, shops, etcetera, from anywhere.

My question: How can I specify WHAT I am looking for? I can only specify the region I'm searching in, which is irrelevant in my case. I would like to be able to tell apple maps to ONLY look for countries, regions, cities, whatever. Is this possible in a way? I have exhausted google for this and found no way thus far.

like image 405
Joris416 Avatar asked Dec 14 '22 00:12

Joris416


1 Answers

Going off what @Trevor said, I found rejecting results where either the title or subtitle have numbers yields pretty good results if you only want cities and towns.

Swift 4.1 code:

// Store this as a property if you're searching a lot.
let digitsCharacterSet = NSCharacterSet.decimalDigits

let filteredResults = completer.results.filter { result in    
    if result.title.rangeOfCharacter(from: digitsCharacterSet) != nil {
        return false
    }

    if result.subtitle.rangeOfCharacter(from: digitsCharacterSet) != nil {
        return false
    }

    return true
}

or more compactly:

let filteredResults = completer.results.filter({ $0.title.rangeOfCharacter(from: digitsCharacterSet) == nil && $0.subtitle.rangeOfCharacter(from: digitsCharacterSet) == nil })
like image 182
Ben Stahl Avatar answered Mar 15 '23 23:03

Ben Stahl