Code Samples Overview

Select platform: Android iOS JavaScript

The SDK demo apps supplied with the Places SDK for iOS demonstrate a number of features including place autocomplete, and place photos. In addition, you can find code snippets on each page of this developer's guide.

Try the SDK demos using CocoaPods

The Places SDK for iOS is available as a CocoaPods pod. CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects.

The Places SDK for iOS supplies a set of SDK demo apps that you can install and run using the pod try command:

Follow these steps to install CocoaPods and try the Places SDK for iOS:

  1. If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal. For details, see the CocoaPods Getting Started guide.
    sudo gem install cocoapods
  2. Get the sample files using one of these two methods:

    Use files from GitHub

    1. Download the code sample archive from GitHub and unpack the archive.
    2. Open a terminal window, navigate to the directory where you expanded the sample files, and drill down into the GooglePlaces directory:

      Swift

      cd maps-sdk-for-ios-samples-master/GooglePlaces-Swift

      Objective-C

      cd maps-sdk-for-ios-samples-master/GooglePlaces
    3. Run the following command:
      pod install
      open GooglePlacesDemos.xcworkspace

    Use CocoaPods v1.6.1

    1. Open a terminal window and install version 1.6.1:
      sudo gem install cocoapods -v1.6.1
    2. Fetch the Google Places files using Cocoapods:
      pod try GooglePlaces

      CocoaPods updates your spec repositories, then opens the SDK demos in a temporary Xcode project, GooglePlacesDemos.xcworkspace.

  3. Enable the Places SDK for iOS for your Google Cloud Console project.
  4. If you don't yet have an API key, follow the instructions to set up a project on the Cloud Console and get an API key. When configuring the key on the Cloud Console, you can specify your app's bundle identifier to ensure that only your app can use the key. The default bundle identifier of the SDK samples app is com.example.GooglePlacesDemos.
  5. Edit the SDKDemoAPIKey file and paste your API key into the appropriate constant. For example:

    Swift

    let placesAPIKey = "YOUR_API_KEY"

    Objective-C

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  6. If Xcode prompts you to unlock the SDKDemoAPIKey file for editing, choose Unlock.
  7. If present, remove the following line, because it's used to register the issue:

    Swift

    #error (Register for API Key and insert here. Then delete this line.”)

    Objective-C

    #error Register your API key and insert here, then delete this line.
  8. Build and run the project again.
  9. If the build fails or the app crashes with an error about your API key when you first run it, make sure that you have provided the keys required in the SDKDemoAPIKey file.
  10. If you are running the GooglePlaces demos, the iOS simulator window will show a list of Places Demos.
  11. If prompted to allow GooglePlacesDemos to access your location, choose Allow.
  12. The sample you chose is now ready to run.

Use code snippets from the developer's guide

Each page in the developer's guide includes code snippets illustrating a particular feature of the API. For example, see the guides for autocomplete, get current place, photos, and other pages in this guide.