Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

mark russo
mark russo
905 Points

Google Map API for SWIFT

I using some sample code for Google Places API that adds a search bar to to the navigation bar. As you type in a place it auto completes. Its working, but when the users makes a selection I want to add that selection to an array I complete. I thought I could add (-> GMSPlaces) to the function and it would return the selected place but I get an error. Here is the code I'm trying to alter. Do you know how I can capture the value elected and use it outside the function: Here is the code:

// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
    func resultsController(resultsController: GMSAutocompleteResultsViewController!,
        didAutocompleteWithPlace place: GMSPlace!) {
            searchController?.active = false
            // Do something with the selected place.
            print("Place name: ", place.name)
            print("Place address: ", place.formattedAddress)
            print("Place attributions: ", place.attributions)       
    }

Here is the full code:

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    var resultsViewController: GMSAutocompleteResultsViewController?
    var searchController: UISearchController?
    var resultView: UITextView?
    var alert:UIAlertController?
    var myPlace:String?

    @IBOutlet weak var myPlaceOut: UILabel!



   /* @IBAction func myAlert(sender: AnyObject) {

        alert = UIAlertController(title: self.myPlace, message: self.myPlace, preferredStyle: UIAlertControllerStyle.ActionSheet)

        self.presentViewController(alert!, animated: true, completion: nil)

    }

*/
    override func viewDidLoad() {
        super.viewDidLoad()

        resultsViewController = GMSAutocompleteResultsViewController()
        resultsViewController?.delegate = self

        searchController = UISearchController(searchResultsController: resultsViewController)
        searchController?.searchResultsUpdater = resultsViewController

        // Put the search bar in the navigation bar.
        searchController?.searchBar.sizeToFit()
        self.navigationItem.titleView = searchController?.searchBar

        // When UISearchController presents the results view, present it in
        // this view controller, not one further up the chain.
        self.definesPresentationContext = true

        // Prevent the navigation bar from being hidden when searching.
        searchController?.hidesNavigationBarDuringPresentation = false



    }



}
// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
    func resultsController(resultsController: GMSAutocompleteResultsViewController!,
        didAutocompleteWithPlace place: GMSPlace!) {
            searchController?.active = false
            // Do something with the selected place.
            print("Place name: ", place.name)
            print("Place address: ", place.formattedAddress)
            print("Place attributions: ", place.attributions)



    }


    func resultsController(resultsController: GMSAutocompleteResultsViewController!,
        didFailAutocompleteWithError error: NSError!){
            // TODO: handle the error.
            print("Error: ", error.description)


    }


    // Turn the network activity indicator on and off again.
    func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController!) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }

    func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController!) {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
}

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Mark,

Can you show us how you were attempting to return the place value? I don't see why this wouldn't work. You can't just add the return type of course; you need to actually return an instance of that type. What error are you getting?