MWPlaceSearch 0.1.0

MWPlaceSearch 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM

Maintained by Masahiro Watanabe.



  • By
  • Masahiro Watanabe

MWPlaceSearch

Did you know Apple officially provides a limit-free place search API called MKLocalSearch ? MWPlaceSearch is an easy-to-use MKLocalSearch library written in Swift.

  • Search with a natural query like ‘Sushi Vancouver’
  • Optionally provide a region as a hint. Doing this results will be more accurate.
  • You will receive a callback when your search is completed. Results include name, URL, phone number, coordinate, address components such as administrative area, city, street name and so on.

Example

import UIKit
import MapKit
import MWPlaceSearch

class ViewController: UIViewController, MWPlaceSearchDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Find a sushi place in Vancouver
        // You can provide a region as a hint. It can also be nil.
        let coordinate = CLLocationCoordinate2DMake(49.282909, -123.1273396) // Vancouver
        let region = MKCoordinateRegionMakeWithDistance(coordinate, 1000.0, 1000.0) // 1km * 1km
        MWPlaceSearch(delegate: self).search("Sushi Vancouver", withRegion: region)
    }

    // This function will be called when a search had done or failed.
    func didFinishSearchingPlace(result: MWPlaceSearchResult) {
        switch result.response {
        case .Success:
            guard let data = result.data else { return }
            data.forEach {
                if let name = $0.name {
                    print("name => \(name)")
                    print("coordinate => \($0.placemark.coordinate.latitude), \($0.placemark.coordinate.longitude)")
                    print("address => \($0.placemark.address)")
                }
            }

        case .Failure:
            ()
        }
    }
}

Requirements

  • Swift 2.2
  • iPhone, iPad
  • iOS 8.0 or later

Installation

pod "MWPlaceSearch"

Author

Masahiro Watanabe, [email protected]

License

MWPlaceSearch is available under the MIT license. See the LICENSE file for more info.