2

I have currently implemented facebook login and taking access of user's location. Like this:

    user.profile.city = auth.info.location

I need only city of user, but location gives city and state. As per permission docs, location is user's city. But it is giving state too.

How to get only city? Is there any way?

user2206724
  • 1,265
  • 3
  • 20
  • 38

3 Answers3

3

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

Try with Graph API Explorer : /me gives the location and home town only with city and state, NOT the zipcode, country, etc.,

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];
        }
    }
});
2

I assume that you are using some gem like omniauth, omniauth-facebook

Try this:

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

since for example

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

using the provided code it will return Olongapo City

Lian
  • 1,597
  • 3
  • 17
  • 30
  • 1
    This does not work. There are many users with locations that do not include countries. For example, a very popular one is "Liverpool." (real example from real user records). Facebook does not enforce structure on the "location" field - it's a page pointer. – Alex Weinstein Dec 24 '14 at 18:20
0

Do this:

SELECT current_location FROM user WHERE uid=USER_ID

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

林果皞
  • 7,539
  • 3
  • 55
  • 70