An objective-c wrapper around the Google Places autocomplete API. Includes sample application emulating the "Maps" app.
Objective-C Ruby
Switch branches/tags
Clone or download
Pull request Compare This branch is 54 commits ahead of spoletto:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Deprecated 😵

Current and final version: 1.0.6.

Thanks for visiting but this repository is no longer maintained. To solve the problem you are facing, please check Place Autocomplete of Google Places API for iOS.

History

SPGooglePlacesAutocomplete was developed by Stephen Poletto in 2012 but wasn't discoverable on CocoaPods. I found his repository in 2013 while searching one to solve same problem for my project.

I thought it would be easier if it could be integrated via CocoaPods, so I cloned his repository, added come updates to the demo project and a .podspec file, then published it onto CocoaPods. Not sure if it was a wise move, maybe I should have contacted Stephen in first place as it sounds like I was taking all the credits.

SPGooglePlacesAutocomplete

SPGooglePlacesAutocomplete is a simple objective-c wrapper around the Google Places Autocomplete API.

The API can be used to provide autocomplete functionality for text-based geographic searches, by returning Places such as businesses, addresses, and points of interest as a user types.

SPGooglePlacesAutocomplete also provides support for converting Place results into CLPlacemark objects for easy mapping with MKMapView.

Screenshots

How To Use It

Requirements

SPGooglePlacesAutocomplete requires a deployment target >= iOS 6.0 and ARC.

Installation

CocoaPods is the recommended way to add SPGooglePlacesAutocomplete to your project.

  1. Add a pod entry pod 'SPGooglePlacesAutocomplete' to to your Podfile.
  2. Run pod install or pod update.
  3. Include SPGooglePlacesAutocomplete wherever you need it with #import "SPGooglePlacesAutocomplete.h".

Performing Queries

Instantiate a new SPGooglePlacesAutocompleteQuery and fill in the properties you'd like to specify.

#import "SPGooglePlacesAutocomplete.h"

...

SPGooglePlacesAutocompleteQuery *query = [[SPGooglePlacesAutocompleteQuery alloc] initWithApiKey:@"YourGoogleAPIKey"];
query.input = @"185 berry str"; // search key word
query.location = CLLocationCoordinate2DMake(37.76999, -122.44696);  // user's current location
query.radius = 100.0;   // search addresses close to user
query.language = @"en"; // optional
query.types = SPPlaceTypeGeocode; // Only return geocoding (address) results.

Remember to provide GoogleAPIKey for browser apps, not iOS ones.

Then, call -fetchPlaces to ping Google's API and fetch results. The resulting array will return objects of the SPGooglePlacesAutocompletePlace class.

[query fetchPlaces:^(NSArray *places, NSError *error) {
    NSLog(@"Places returned %@", places);
}];

If you need to update the query (for instance, as the user types), simply update the appropriate properties and call -fetchPlaces again. Any outstanding requests will automatically be cancelled and a new request with the updated properties will be issued.

Resolving Places to CLPlacemarks

The Google Places Autocomplete API will return the names of Places that match your query. It will not, however, return lat-long information about these results. SPGooglePlacesAutocomplete handles this by resolving Place results to placemarks. Simply call -resolveToPlacemark on a SPGooglePlacesAutocompletePlace:

[query fetchPlaces:^(NSArray *places, NSError *error) {
    SPGooglePlacesAutocompletePlace *place = [places firstObject];
    if (place) {
        [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
            NSLog(@"Placemark: %@", placemark);
        }];
    }
}];

When searching for "geocode" (address) Places, the library utilizes CLGeocoder to geocode the address. When searching for "establishment" (business) Places, the library will automatically ping The Google Places API to fetch the details needed to geolocate the business.

Sample Code

For an example of how to use SPGooglePlacesAutocomplete, please see the included example project. I put a functional Google API key in the project, please don't use it elsewhere!

Please also note that using the Place Autocomplete API with Apple maps is against Google's terms of service. From the documentation: "You can use Place Autocomplete even without a map. If you do show a map, it must be a Google map. When you display predictions from the Place Autocomplete service without a map, you must include the 'powered by Google' logo.."