Facebook details how it built the Location API

Facebook has explained how eight engineers worked for a year and half building the new location features that make location tagging universal on the social network.
Written by Emil Protalinski, Contributor

Earlier this week, Facebook brought over status update features to apps. Developers now have the ability to let users add friends, location, and share photos directly from apps. The social networking giant today outlined details behind broadening the Location API. The company says it spent the past year and a half making location tagging universal so that your check-ins can appear on Timeline, and your apps can engage with where you've been and where you want to go next. Eight Facebook engineers built the new location features and the Timeline map, working with the Timeline, Places, mobile, search, and tagging teams.

In August 2011, Facebook killed its Places product, but also rolled out new location features. The company says it focused on making it as easy to share your location, no matter what kind of updates you were posting. To do this, Facebook changed the concept of location from being a specific type of information you broadcast via your smartphone, to metadata you can add to any Facebook item from any device. This includes being able to search for places in the vicinity of your current location.

First, Facebook asked for permission to use the GPS API to fetch your current location. If granted, Facebook divided the globe into a large number of small polygons, and all "places" in the world were stored in memory and indexed to their respective polygons. Unfortunately, this infrastructure restricted you from incorporating location into your Facebook profile. After Facebook starting letting you add location to any content shared on the site, searching for places was much harder to process since you could potentially be searching for any place in the world, irrespective of your current location.

To cope with this huge dataset, Facebook assigned four different polygons of increasing size to each place. All "places" in the world were thus indexed by each of these polygons and also by the first three letters for each word in the place's name. Developing the universal search infrastructure allowed Facebook to provide users with the ability to search for places on any device (not just smartphones), improving search quality with location if it's available.

Given a query, places matching the first three letters are fetched and intersected with the list of places in all four polygons containing your current location. Depending on the use case, results can be biased toward places closer to you or provide completely global search. Since the candidate set of places increases significantly in this new system, Facebook stores a large set of features to improve ranking, including check-ins and Likes received by the place as well as the company's estimate for the opening/closing time for the place. Finally, Facebook runs models to select the most relevant places based on these features.

When Facebook created Timeline, the timeline map (picture above) was integral: it lets you see every place you've checked into, as well as the photos and statuses you uploaded at those places. Unlike most Facebook pages, the timeline map requires fetching every piece of content that the user has created with location, not just the first x chronological items. To manage this data load, Facebook built infrastructure to farm out data fetching to multiple servers. On every page load, a single server fetches the IDs of all pieces of content that can be displayed for the current user, then breaks up this data into smaller chunks, and each chunk is sent in a request to another server to actually fetch the data and do privacy checks. The results from these servers are combined to create the timeline map display.

Here's what Facebook had to say about aggregation algorithms for location pins, which I find the most fascinating part:

Building the logic for aggregating pins on the map was another interesting challenge because we wanted to choose an optimal algorithm for aggregation while ensuring that we show as many distinct pins as possible. One of our first, more optimistic options was to aggregate any two points that overlapped, but this would distort the order of the pins. Another option we explored was to always aggregate the pins that are closest to each other to make the solution deterministic, but this turned out to be too slow. Instead, an engineer on our team, Jon McCord, wrote an algorithm to deterministically draw these pins. At any point in time, two pins were aggregated only if they were each other's closest neighbor. This was compared with all other pins to find another pin that satisfied this property. If found, the two pins were aggregated into a single pin. This process was followed until no overlapping pins remained.

Last but not least, Facebook talked about location suggestions. The company created systems that iterate through all the albums uploaded by a user and search the title and location fields for text that could signify the place at which the album could have been taken. The team also built a special flow allowing you to seamlessly tag your existing photos with location and immediately add them to your map.

Now it's up to the app developers to take advantage of what Facebook is offering. It won't be long before the social network automatically gives you recommendations of new places to check out based on where your friends have been.

See also:

Editorial standards