Search for address using SWIFT

前端 未结 1 1708
太阳男子
太阳男子 2021-01-21 08:03

In my app I need the user to enter his home address, but I can\'t get any way that the addresses that are shown are only in the users region, and are full addresses, like

相关标签:
1条回答
  • 2021-01-21 09:05

    You can define a local search completer:

    let completer = MKLocalSearchCompleter()
    

    And then supply the query fragment:

    completer.delegate = self
    completer.region = MKCoordinateRegion(center: currentCoordinate, latitudinalMeters: 10_000, longitudinalMeters: 10_000)
    completer.queryFragment = "300 S Orange"
    

    And implement the MKLocalSearchCompleterDelegate protocol

    extension ViewController: MKLocalSearchCompleterDelegate {
    
        func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
            let addresses = completer.results.map { result in
                result.title + ", " + result.subtitle
            }
    
            // use addresses, e.g. update model and call `tableView.reloadData()
        }
    
    }
    

    And that yields:

    ["300 S Orange Grove Blvd, Pasadena, CA, United States",
     "300 S Orange Ave, Monterey Park, CA, United States",
     "300 S Orange St, Glendale, CA, United States",
     "300 S Orange Dr, Los Angeles, CA, United States",
     "300 S Orange Ave, Azusa, CA, United States",
     "300 S Orange Grove Ave, Los Angeles, CA, United States",
     "300 S Orange Ave, Brea, CA, United States",
     "300 S Orange Ave, Fullerton, CA, United States",
     "300 S Orange St, Orange, CA, United States",
     "300 S Orange Ave, Rialto, CA, United States",
     "300 S Orange Ave, Fallbrook, CA, United States",
     "300 S Orange St, Escondido, CA, United States",
     "300 S Orange Ave, El Cajon, CA, United States",
     "300 S Orange Cir, Tulare, CA, United States",
     "300 S Orange Ave, Exeter, CA, United States"]
    

    Or else, you can do a local search:

    let request = MKLocalSearch.Request()
    request.region = MKCoordinateRegion(center: currentCoordinate, latitudinalMeters: 10_000, longitudinalMeters: 10_000)
    request.naturalLanguageQuery = "300 S Pumpkin"
    
    let formatter = CNPostalAddressFormatter()
    formatter.style = .mailingAddress
    
    let search = MKLocalSearch(request: request)
    search.start { response, error in
        let addresses = response?.mapItems.compactMap { item -> String? in
            return item.placemark.postalAddress.flatMap {
                formatter.string(from: $0).replacingOccurrences(of: "\n", with: ", ")
            }
        }
        print(addresses)
    }
    

    Now, I’m using the Contacts framework’s formatter (which also exposes the postalAddress property on the placemark), but you can format these any way you want. But, assuming it finds matches, that shows something like:

    ["300 S Pumpkin Blvd, Los Angeles, CA  92108, United States",
     "300 S Pumpkin Pkwy, Los Angeles, CA  92103, United States",
     "300 S Pumpkin Dr, San Gabriel, CA  91776, United States"])
    

    Now, whether you want the FormattedAddressLines, or extract the individual components from the addressDictionary is up to you, but hopefully this illustrates the idea.

    0 讨论(0)
提交回复
热议问题