Facebook Developers
DocsToolsSupportNewsApps
Log In
  • Social Plugins
  • Facebook Login
  • Open Graph
  • Facebook APIs
  • Games
  • Media
  • Payments
  • App Center
  • Promote Your App
  • iOS
  • Android
  • Web
  • Technology Partners
  • Topics
    • Facebook SDK for iOS
  • Getting Started
    • Getting Started with the Facebook SDK for iOS
    • Build, Distribute and Promote
  • iOS SDK Tutorial
    • Facebook iOS SDK Tutorial
    • 1 - Authenticate Using Facebook Login
    • 2 - Personalize
    • 3 - Show Friends
    • 4 - Show Nearby Places
    • 5 - Publish Open Graph Story
  • Games Tutorial
    • iOS Games Tutorial
    • 1 - Authenticate
    • 2 - Personalize
    • 3 - Invites and Requests
    • 4 - Bragging and News Feed
    • 5 - Publish Open Graph Story
  • Upgrading SDKs Tutorials
    • Upgrading from 3.2 to 3.5
    • Upgrading from 3.1 to 3.2
    • Upgrading from 3.0 to 3.1
    • Upgrading from 2.0 to 3.1

4 - Show Nearby Places

Documentation › Facebook iOS SDK Tutorial › 4 - Show Nearby Places

This tutorial outlines how to add the user's location to a story with the Facebook SDK for iOS. When you've finished these steps, your flow should look like this:

To display the place picker, make use of the FBPlacePickerViewController Facebook SDK object. To use this object, first initialize an instance through the initWithNibName:bundle: method. Once you have an instance, set the session property to tie it to the authenticated user. Then, set the locationCoordinate property to the coordinate information from the CoreLocation framework's location update. Then, set the delegate method to define the object that handles the FBPlacePickerDelegate notifications. Finally, call the loadData to initiate the API call to get places nearby and display them with the FBPlacePickerViewController.

In this tutorial, you'll implement the FBPlacePickerDelegate method placePickerViewControllerSelectionDidChange: that is notified when a user selects a place. That delegate method will pass the FBPlacePickerViewController instance and you'll use the selection property to retrieve the place. The selected place will be a FBGraphPlace, a strongly typed object that represents a place.

This tutorial walks through the following:

  • Step 1: Add the Core Location Framework
  • Step 2: Get the User's Current Location
  • Step 3: Show the Places Picker
  • Step 4: Display the Selected Place
  • Next Steps
  • Related Samples

Note: Before you start this tutorial, make sure you've run through the Show Friends Tutorial.


Step 1: Add the Core Location Framework

Add the Core Location Framework to the project:


Step 2: Get the User's Current Location

Use the Core Location Framework to get the user's current location. If you're not familiar with how to set that up, review the Apple documentation.

Start the location manager with Core Location when the view loads. When you get an accurate location reading, set up the framework to update the places view controller in the steps that follow. The following changes apply to the SCViewController implementation file.

First, make sure the SCViewController class conforms to the CLLocationManagerDelegate protocol.

Next, create a property to represent an instance of the CLLocationManager class.

@property (strong, nonatomic) CLLocationManager *locationManager;

Synthesize the new property:

@synthesize locationManager = _locationManager;

Next, start the location updates in the viewDidLoad method by adding this code to the end of the method:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// We don't want to be notified of small changes in location, 
// preferring to use our last cached results, if any.
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];

When the view is deallocated, nil out the location manager delegate. Add the following to the dealloc method:

Then, to nil out the delegate the dealloc method is called by adding the following to that method:

_locationManager.delegate = nil;

Finally, implement the CLLocationManagerDelegate methods for handling location updates and errors:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation 
{
    if (!oldLocation ||
        (oldLocation.coordinate.latitude != newLocation.coordinate.latitude && 
         oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {

        // To-do, add code for triggering view controller update
        NSLog(@"Got location: %f, %f", 
              newLocation.coordinate.latitude, 
              newLocation.coordinate.longitude);
    }
}

- (void)locationManager:(CLLocationManager *)manager 
didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}

Build and run the project to make sure it runs without errors. You should see an alert requesting to use your current location:

Once you grant this permission, you should see a debug log of the current location's latitude and longitude based on the NSLog you added to the locationManager:didUpdateToLocation:fromLocation: delegate method. Here's an example:

2012-05-20 23:11:35.622 Scrumptious[26225:f803] Got location: 37.444574, -122.162776

Remove the debug NSLog statement from the locationManager:didUpdateToLocation:fromLocation: method after you verify it runs successfully.


Step 3: Show the Places Picker

Now that you have the current location, use it with the FBPlacePickerViewController object to show the place picker when the user taps on the ''Where are you?'' menu option.

First, create a property for the FBPlacePickerViewController object:

@property (strong, nonatomic) FBPlacePickerViewController *placePickerController;

Synthesize this property:

@synthesize placePickerController = _placePickerController;

Next, make sure that the view is released in the viewDidUnload method, by adding this to the end of the method:

self.placePickerController = nil;

Next, modify the tableView:didDeselectRowAtIndexPath: delegate method to set up the place picker. Add a new case statement that will do the following - initialize an instance of FBPlacePickerViewController if necessary, set the required properties for the FBPlacePickerViewController instance and finally push the FBPlacePickerViewController view controller:

case 1:
    if (!self.placePickerController) {
        self.placePickerController = [[FBPlacePickerViewController alloc] 
                                         initWithNibName:nil bundle:nil];
        self.placePickerController.title = @"Select a restaurant";
    }
    self.placePickerController.locationCoordinate = 
    self.locationManager.location.coordinate;
    self.placePickerController.radiusInMeters = 1000;
    self.placePickerController.resultsLimit = 50;
    self.placePickerController.searchText = @"restaurant";

    [self.placePickerController loadData];
    [self.navigationController pushViewController:self.placePickerController 
                                         animated:true];
    break;

Build and run the project to make sure it runs without errors. Once you see the home menu selection, select the ''Where are you?'' menu option. You should see a list of places nearby. Selecting a restaurant and tapping the Back button should do nothing at this point. You will add the functionality that saves the selected place next.


Step 4: Display the Selected Place

Add FBPlacePickerDelegate to the list of protocols that the SCViewController conforms to.

Modify the tableView:didSelectRowAtIndexPath: method to set the delegate that handles the nearby place selection. Do this by adding the following statement right after the FBPlacePickerViewController object is initialized:

self.placePickerController.delegate = self;

When the view is deallocated, nil out the place picker delegate by adding this code to the end of the dealloc method:

_placePickerController.delegate = nil;

Next, create a property that will hold the selected place:

@property (strong, nonatomic) NSObject<FBGraphPlace>* selectedPlace;

Synthesize this new property:

@synthesize selectedPlace = _selectedPlace;

Modify the updateSelections method to take care of the place selection and display it under the ''Where are you?'' menu. Add this code to the end of the method:

[self updateCellIndex:1 withSubtitle:(self.selectedPlace ?
                                     self.selectedPlace.name :
                                     @"Select One")];

Finally, implement the FBPlacePickerDelegate method that is notified when a place is selected. In this method, call the updateSelections method to display the selected restaurant:

- (void)placePickerViewControllerSelectionDidChange:
(FBPlacePickerViewController *)placePicker
{
    self.selectedPlace = placePicker.selection;
    [self updateSelections];
    if (self.selectedPlace.count > 0) {
        [self.navigationController popViewControllerAnimated:true];
    }
}

Build and run the project to make sure it runs without errors. Tap the ''Where are you?'' menu selection and select a location from the restaurant list. Tap the Back button. You should see the place you selected displayed in the ''Where are you?'' row.


Next Steps

Complete the tutorial after learning how to publish an Open Graph action.


Related Samples

  • PlacePickerSample
Updated about a week ago
Facebook © 2013 · English (US)
AboutAdvertisingCareersPlatform PoliciesPrivacy Policy