GooglePlacesAutocomplete 0.3.0

GooglePlacesAutocomplete 0.3.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2015
SPMSupports SPM

Maintained by Howard Wilson.




  • By
  • Howard Wilson

A simple Google Places API autocompleting address entry view for iOS devices.

There are already a couple of solutions out there for this. GooglePlacesAutocomplete is different because it is 100% Swift, and aims to provide the simplest possible method of entering validated, autocompleted addresses.

No attempt has been made to integrate MapKit since displaying Google Places on a non-Google map is against their terms of service.


Requirements

  • iOS 7.0+
  • XCode 6.2+ / Swift 1.2 (for XCode 6.1 / Swift 1.1 use version 0.2.1)

Installation

Embedded frameworks require a minimum deployment target of iOS 8.

To use GooglePlacesAutocomplete with a project targeting iOS 7, you must include the source files directly in your project. See the 'manual installation' section for instructions.

Manual

Simply copy GooglePlacesAutocomplete.swift and GooglePlacesAutocomplete.xib to your project.

Note: Don't forget to add the PoweredByGoogle image to your xcassets.

Usage

import GooglePlacesAutocomplete // Not required when including source files directly in project

let gpaViewController = GooglePlacesAutocomplete(
  apiKey: "[YOUR GOOGLE PLACES API KEY]",
  placeType: .Address
)

gpaViewController.placeDelegate = self // Conforms to GooglePlacesAutocompleteDelegate

presentViewController(gpaViewController, animated: true, completion: nil)

GooglePlacesAutocompleteDelegate supports three methods:

  • placesFound(places: [Place]): Invoked whenever the Google Places API is called
  • placeSelected(place: Place): Invoked when a new place is selected
  • placeViewClosed(): Invoked when the view is closed

Here's a complete example.

Place Details

From Google's documentation: "you can request more details about a particular establishment or point of interest by initiating a Place Details request. A Place Details request returns more comprehensive information about the indicated place such as its complete address, phone number, user rating and reviews."

place.getDetails { details in
  println(details.name)       // Convenience accessor for name
  println(details.latitude)   // Convenience accessor for latitude
  println(details.longitude)  // Convenience accessor for longitude
  println(details.raw)        // Complete JSON data (see below)
}

/*
  [
    status: OK,
    result: {
      "address_components" = (
        {
          "long_name" = Paris;
          "short_name" = Paris;
          types = (
            locality,
            political
          );
        },
        ...
      );
      geometry = {
        location = {
          lat = "48.856614";
          lng = "2.3522219";
        };
  ...
*/

See the documentation for full response details.

Styling

The UINavigationController appearance can also easily be changed, for example:

gpaViewController.navigationBar.barStyle = UIBarStyle.Black
gpaViewController.navigationBar.translucent = false
gpaViewController.navigationBar.barTintColor = UIColor(red: 0.11, green: 0.27, blue: 0.53, alpha: 1.0)
gpaViewController.navigationBar.tintColor = UIColor.whiteColor()
gpaViewController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Zapfino", size: 16.0)!]

Also, to change the contents of the title bar:

gpaViewController.navigationItem.title = "Enter City"
gpaViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: gpaViewController, action: "close")

Contributing

  1. Fork it ( https://github.com/watsonbox/ios-google-places-autocomplete/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request