You're all set!

To start developing, please head over to our developer documentation.

Activate the Google Places API for iOS

To get you started we'll guide you through the Google Developers Console to do a few things first:

  1. Create or choose a project
  2. Activate the Google Places API for iOS
  3. Create appropriate keys
Continue

Current Place

Using the Google Places API for iOS, you can discover the place where the device is currently located. That is, the place at the device's currently-reported location. Examples of places include local businesses, points of interest, and geographic locations.

  1. Request location authorization
  2. Usage limits
  3. Get the current location
  4. Display attributions in your app

Request location authorization

If your app uses GMSPlacesClient currentPlaceWithCallback:, you must request permission to use location services. Add the NSLocationWhenInUseUsageDescription key to your Info.plist file, to define the string informing the user why you need the location services. For example:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Show your location on the map</string>

If you want to call GMSPlacesClient currentPlaceWithCallback: when the app is in the background, without triggering a confirmation dialog, take the following steps prior to making the call:

  1. Add the NSLocationAlwaysUsageDescription key to your Info.plistfile.
  2. Call requestAlwaysAuthorization on any instance of CLLocationManager before calling the method.

Request authorization from CLLocationManager as follows:

Swift

    locationManager.requestAlwaysAuthorization()
    

Objective-C

    [self.locationManager requestAlwaysAuthorization];
    

Usage limits

Use of the GMSPlacesClient currentPlaceWithCallback: method is subject to tiered query limits. See the documentation on usage limits.

Get the current location

To find the local business or other place where the device is currently located, call GMSPlacesClient currentPlaceWithCallback:. Include a callback method to handle the results.

The API invokes the specified callback method, passing in an array of GMSPlaceLikelihood objects.

Each GMSPlaceLikelihood object represents a place. For each place, the result includes an indication of the likelihood that the place is the right one. A higher value means a greater probability that the place is the best match. The buffer may be empty, if there is no known place corresponding to the device location.

The following code sample retrieves the list of places where the device is most likely to be located, and logs the name, likelihood, and other details for each place.

Swift

placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
  if let error = error {
    print("Pick Place error: \(error.localizedDescription)")
    return
  }

  if let placeLikelihoodList = placeLikelihoodList {
    for likelihood in placeLikelihoodList.likelihoods {
      let place = likelihood.place
      print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)")
      print("Current Place address \(place.formattedAddress)")
      print("Current Place attributions \(place.attributions)")
      print("Current PlaceID \(place.placeID)")
    }
  }
})

Objective-C

  [_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
  if (error != nil) {
    NSLog(@"Current Place error %@", [error localizedDescription]);
    return;
  }

  for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {
    GMSPlace* place = likelihood.place;
    NSLog(@"Current Place name %@ at likelihood %g", place.name, likelihood.likelihood);
    NSLog(@"Current Place address %@", place.formattedAddress);
    NSLog(@"Current Place attributions %@", place.attributions);
    NSLog(@"Current PlaceID %@", place.placeID);
  }

}];
  

Notes about the likelihood values:

  • The likelihood provides a relative probability of the place being the best match within the list of returned places for a single request. You can't compare likelihoods across different requests.
  • The value of the likelihood will be between 0 and 1.0.
  • The sum of the likelihoods in a returned array of GMSPlaceLikelihood objects is always less than or equal to 1.0. Note that the sum isn't necessarily 1.0.

For example, to represent a 55% likelihood that the correct place is Place A, and a 35% likelihood that it's Place B, the likelihood array has two members: Place A with a likelihood of 0.55 and Place B with a likelihood of 0.35.

Display attributions in your app

When your app displays information obtained from GMSPlacesClient currentPlaceWithCallback:, the app must also display attributions. See the documentation on attributions.

Send feedback about...

location_on
Google Places API for iOS