我目前實現了Facebook登錄並獲取用戶位置的訪問權限。像這樣:Facebook圖形API:獲取用戶所在城市(位置)
user.profile.city = auth.info.location
我只需要城市的用戶,但位置給了城市和州。根據許可文檔,位置是用戶的城市。但它也是一種狀態。
如何獲得唯一的城市?有什麼辦法嗎?因爲例如
user.profile.city = auth.info.location.split(",")[0]
:
我目前實現了Facebook登錄並獲取用戶位置的訪問權限。像這樣:Facebook圖形API:獲取用戶所在城市(位置)
user.profile.city = auth.info.location
我只需要城市的用戶,但位置給了城市和州。根據許可文檔,位置是用戶的城市。但它也是一種狀態。
如何獲得唯一的城市?有什麼辦法嗎?因爲例如
user.profile.city = auth.info.location.split(",")[0]
:
我假設你使用的是一些gem
像omniauth
,omniauth-facebook
試試這個
`auth.info.location` value = location: Olongapo City, Philippines
使用所提供的代碼,它會return Olongapo City
哇,簡單而好看:-) – user2206724 2013-04-26 09:14:58
這不行。有許多用戶的位置不包括國家/地區。例如,一個非常流行的是「利物浦」。 (真實用戶記錄的實例)。 Facebook不會在「位置」字段上強制執行結構 - 它是頁面指針。 – 2014-12-24 18:20:42
這樣做:
SELECT current_location FROM user WHERE uid=USER_ID
https://developers.facebook.com/tools/explorer/
嘗試用圖形API瀏覽器:/我 只有城市和國家賦予的位置和家鄉,沒有郵編,國家,
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];
}
}
});
難道你不能把這座城市解析爲「城市+州」嗎? – Madbreaks 2013-04-25 19:52:08
@Madbreaks不知道該怎麼做。對不起,新的ror。 – user2206724 2013-04-25 20:04:11