Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,019 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?