Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get latitude/longitude of friend's current_location using Graph FQL

I'm trying to get the latitude/longitude of all of a user's friends using a single API call. I believe I need to write a multi-query FQL statement, but I can't get the syntax correct.

I believe the two queries need to be something like the following:

'"friends":"SELECT uid,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"'

'"location":"SELECT location FROM page WHERE id IN (SELECT current_location_id FROM #friends)"';

My problem is that somehow I need to get the ID of the current_location in the first query so that I can reference it in the second query, but I only know how to get an array that contains the id.

Thanks for any help!

Clarification:

I'm not trying to get check-ins of friends. Instead I want to graph the "current_location" (i.e. where they live) of all of a user's friends on a Geo map. I can get a user's "current_location" with the following FQL query:

"SELECT uid,sex,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"

This query returns the following array:

[current_location] => Array
            (
                [city] => New York
                [state] => New York
                [country] => United States
                [zip] => 
                [id] => 108424279189115
                [name] => New York, New York
            )

Which does not contain the longitude and latitude of the of the current_location (New York in the above example)

Therefore I now need to take the ID of the current_location (e.g. 108424279189115) and run a second FQL query such as:

"SELECT location,name FROM page WHERE page_id = 108424279189115)"

To get the latitude and longitude of the current_location. Currently, I have a working code that runs the first FQL query, then extracts page_id in php, and then runs a second FQL query to get the current_location latitude/longitude. However, for performance reasons I would like to run only one multi-query if this is possible.

like image 888
pyro70 Avatar asked Jan 13 '12 05:01

pyro70


3 Answers

Edit:

The query should work by adjusting the second query to:

'"location":"SELECT location FROM page WHERE id IN (SELECT current_location.id FROM #friends)"';

When working with an array type in the FB API you can access the value in any query by doing array.value

Also, if all you need is the page's id, you'll probably get better performance by doing current_location.id in your first query


You're going to want to use a fql.multiquery, there is a similair answer here that can be tailored to your needs: Facebook Graph API get all users and their status

The FQL documentation on multiquery is located here: http://developers.facebook.com/docs/reference/fql/

Also are you trying to get the lat/long of a checkin object? If so you need to take a look at this resource: http://developers.facebook.com/docs/reference/fql/checkin/

Edit:

I'm not sure that what you are trying to do is possible. I just looked through the api and tested some queries, the only way you could get the lat/long from a friend seems to be based on a checkin, from there you can get details on the page if necessary... Even looking at your second query, current_location_id is not referenced anywhere in the page table, and selecting location from the page table is going to give you location info on the place that it pertains to. Maybe you could elaborate more on what you're trying to accomplish.

Here is a multiquery statement I wrote in PHP using FQL (it will select the coordinates of all your friends check ins, then from there get details on the place of the checkin pertaining to that friend):

try{
    $multiQuery = "{
        'query1':'SELECT coords, tagged_uids, page_id FROM checkin WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())',
        'query2':'SELECT name, description FROM page WHERE page_id IN (SELECT page_id FROM #query1)'
    }";

    $param = array(       
         'method' => 'fql.multiquery',       
         'queries' => $multiQuery,       
         'callback' => ''
    );       
    $queryresults = $facebook->api($param);
        print_r($queryresults);
    }
catch(Exception $o){
    print_r($o);
}
like image 138
Jeff Wooden Avatar answered Oct 16 '22 13:10

Jeff Wooden


fql?q=SELECT location,name FROM page WHERE page_id in (SELECT current_location.id from user where uid in(select uid2 from friend where uid1=me()))

this will do the magic for you

like image 43
user1583465 Avatar answered Oct 16 '22 12:10

user1583465


facebook.batch_fql({
    'query1': "SELECT uid, name, current_location.id FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())",
    'query2': "SELECT page_id, name, description, location FROM page WHERE page_id IN (SELECT current_location.id FROM #query1)"
})

will return two responses

one will be a list of friends UIDs, names and their current locations IDs

the other will be a list of unique location IDs with their name, description and geolocation info

EDIT: if you query for current_location using FQL, you'll get the full latlong in the first response and you don't even need the second one

like image 1
allixsenos Avatar answered Oct 16 '22 12:10

allixsenos