使用Facebook图形API,是否可以从位置对象获取经度和纬度?

时间:2014-04-16 20:33:24

标签: php html facebook facebook-graph-api

其余代码可以运行并返回我正在寻找的值。代码的最后两行不正确。根据我所了解的基于Facebook的文档(我可能会误读某些内容),位置对象会存储经度和纬度,但我无法弄清楚如何访问这些东西。

以下是我的代码的摘录:

 $user_profile = $facebook->api('/me','GET');
 echo "Name: " . $user_profile['name'] . "<br />";
 echo "Gender: : "  . $user_profile['gender'] . "<br />";
 echo "Birthday: " . $user_profile['birthday']. "<br />";
 echo "Religion: " . $user_profile['religion']. "<br />";
 echo "Hometown: " . $user_profile['hometown']['name']. "<br />";
 $locationName = $user_profile['location']['name'];
 echo "Location: " . $location . "<br />";
 echo "Longitude: " . $user_profile['location']['']. "<br />";
 echo "Latitude: " . $user_profile['location']['']. "<br />";

2 个答案:

答案 0 :(得分:2)

对我来说,我目前收回这些数据

  "location": {
    "id": "109873549031485", 
    "name": "Longmont, Colorado"
  }, 

然后您可以使用

获取ID
id =['location']['id']

然后再次调用图形

$user_profile = $facebook->api('/' + id,'GET');

然后将提供具有lat / long

的对象
 "location": {
    "latitude": 40.1672, 
    "longitude": -105.101
  }, 

答案 1 :(得分:2)

您可以使用FQL查询来实现此目的。 lat / lng字段不能通过Graph API获得。因此,您可以像下面这样进行查询(假设您已为这些字段/连接请求了相应的权限):

select name, sex, birthday, religion, hometown_location.city, hometown_location.latitude, hometown_location.longitude, current_location.city, current_location.latitude, current_location.longitude from user where uid=me()

给你类似的东西

{
  "data": [
    {
      "name": "TestUser",
      "sex": "male",
      "birthday": "February 7, 1980",
      "religion": "Catholic",
      "hometown_location": {
        "city": "berlin",
        "latitude": 42.2544,
        "longitude": 8.84583
      },
      "current_location": {
        "city": "Paris",
        "latitude": 55.25,
        "longitude": 12.4
      }
    }
  ]
}

您需要将代码更改为

$user_profile= $facebook->api( array(
                         'method' => 'fql.query',
                         'query' => 'select name, sex, birthday, religion, hometown_location.city, hometown_location.latitude, hometown_location.longitude, current_location.city, current_location.latitude, current_location.longitude from user where uid=me()',
                     ));
echo "Name: " . $user_profile['name'] . "<br />";
echo "Gender: : "  . $user_profile['sex'] . "<br />";
echo "Birthday: " . $user_profile['birthday']. "<br />";
echo "Religion: " . $user_profile['religion']. "<br />";
echo "Hometown City: " . $user_profile['hometown_location']['city']. "<br />";
echo "Hometown Lat: " . $user_profile['hometown_location']['latitude']. "<br />";
echo "Hometown Lng: " . $user_profile['hometown_location']['longitude ']. "<br />";
echo "Current City: " . $user_profile['current_location']['city']. "<br />";
echo "Current Lat: " . $user_profile['current_location']['latitude']. "<br />";
echo "Current Lng: " . $user_profile['current_location']['longitude ']. "<br />";