首页 文章

Facebook Graph API:获取用户城市(位置)

提问于
浏览
2

我目前已经实现了facebook登录并访问了用户的位置 . 像这样:

user.profile.city = auth.info.location

我只需要用户的城市,但是位置给出城市和州 . 根据权限文档,location是用户的城市 . 但它也给了国家 .

如何只获得城市?有什么办法吗?

3 回答

  • 0

    我假设你正在使用 gem ,如 omniauthomniauth-facebook

    试试这个:

    user.profile.city = auth.info.location.split(",")[0]
    

    因为例如

    `auth.info.location` value = location: Olongapo City, Philippines
    

    使用提供的代码,它将 return Olongapo City

  • 2

    https://developers.facebook.com/tools/explorer/

    尝试使用Graph API Explorer:/ me仅为城市和州提供位置和家乡,而不是邮政编码,国家/地区等,

    FB.api('/me', function (json) {
        var fid = json.id;
        var sCity = '';
            var sState = '';
        if (json.location !== undefined && json.location.name !== undefined) {
                var sLoc = json.location.name;
            var aLoc = sLoc.split(',');
            if (aLoc.length > 0) {
                sCity = aLoc[0];
            }
            if (aLoc.length > 1) {
                sState = aLoc[1];
            }
        }
    });
    
  • 3

    做这个:

    SELECT current_location FROM user WHERE uid=USER_ID
    

    示例:https://developers.facebook.com/tools/explorer?fql=SELECT%20current_location%20FROM%20user%20WHERE%20uid%3D4

相关问题