Skip to content

Instantly share code, notes, and snippets.

@fongd
Last active August 8, 2016 16:47
Show Gist options
  • Select an option

  • Save fongd/e33eb4dcb9643fa2dc11e8b4fa2e6450 to your computer and use it in GitHub Desktop.

Select an option

Save fongd/e33eb4dcb9643fa2dc11e8b4fa2e6450 to your computer and use it in GitHub Desktop.

Revisions

  1. fongd revised this gist Aug 8, 2016. 2 changed files with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions GooglePlacesSearchModel.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    class GooglePlacesSearchModel {
    static var serialQueue: dispatch_queue_t = dispatch_queue_create("com.example.Search.GooglePlaces", DISPATCH_QUEUE_SERIAL)
    [...]
    func executeSearch(searchText: String, mapView: GMSMapView?, completion: ([AnyObject]) -> Void) -> () {
    // get bounds of mapView to weight our autocomplete query accordingly
    var bounds: GMSCoordinateBounds?
    if let visibleRegion = mapView?.projection.visibleRegion() {
    bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)
    }

    // get centre point of our mapView for use with nearby search
    if let centerCoordinates = getMapViewCenterCoordinate(mapView),
    let radius = getViewRadius(mapView)
    where shouldDoNearbySearch(searchText) {

    dispatch_async(GooglePlacesSearchModel.serialQueue, {
    self.doNearbySearch(searchText, location: centerCoordinates, radius: radius) { (results) in
    // return [Dictionary<String, AnyObject>]
    completion(results)
    }
    })
    } else {
    // autocomplete query
    dispatch_async(GooglePlacesSearchModel.serialQueue, {
    self.doAutocompleteQuery(searchText, bounds: bounds) { (results) in
    // return [GMSAutocompletePrediction]
    completion(results)
    }
    })
    }
    }
    }
    File renamed without changes.
  2. fongd created this gist Aug 8, 2016.
    15 changes: 15 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    func updateSearchResultsForSearchController(searchController: UISearchController) {
    if let searchText = searchController.searchBar.text where searchText != "" {
    // retrieve results from Google
    self.googlePlacesSearchModel.executeSearch(searchText, mapView: self.mapView) { (completion) in
    dispatch_async(dispatch_get_main_queue(), {
    self.googlePlacesResults = completion
    print("Search text was: \(searchText)")
    print(completion)
    let indexSection = NSIndexSet(index: tableSections.googlePlaces)
    self.tableView.reloadData()
    self.tableView.reloadSections(indexSection, withRowAnimation: .Automatic)
    })
    }
    }
    }