問題描述
我正在開展一個包含季節性內容的項目,我們正在考慮確定用戶的位置,以確定適合他們的季節.這樣做的明顯方法是對他們的 IP 進行地理定位,然后獲取緯度.> 0 是北半球,<0 是南方.
I'm working on a project which includes seasonal content, and we're thinking of determining the user's location to work out what season it is for them. The obvious way of doing this is to Geo-locate their IP, then grab the latitude. > 0 is Northern hemisphere, and < 0 is Southern.
我很高興這樣做 - 雖然將 IP 精確定位到確切位置似乎有點浪費,只是為了確定它們在地球的哪一半 - 但我想我會扔掉它以防萬一有人有任何可能縮短流程的技巧.
I'm happy to go that way - though it seems a bit of a waste to pinpoint an IP to an exact location, just to determine which half of the planet they're on - but I thought I'd throw it out there in case anyone has any tricks which might shortcut the process.
請求標頭,可以在客戶端用 JS 提取的東西,很容易獲得 - 我只是認為它沒有任何幫助.
Request headers, stuff that can be extracted with JS on the client, it's all easy enough to get - I just don't think any of it helps.
推薦答案
我先看看客戶的時鐘——如果客戶的日歷中存在夏令時,你可以判斷他是在赤道以北還是以南.
I'd first check the client's clock- if daylight savings exists in the client's calendar, you can tell if he is north or south of the equator.
如果沒有dst信息,可以使用geolocation,
if there is no dst information, you can use geolocation,
或者詢問用戶是否在赤道以南...
or ask the user if he is south of the equator...
window.whatHemisphere= (function(){
var y= new Date().getFullYear();
if(y.getTimezoneOffset()==undefined) return null;
var jan= -(new Date(y, 0, 1, 0, 0, 0, 0).getTimezoneOffset()),
jul= -(new Date(y, 6, 1, 0, 0, 0, 0).getTimezoneOffset()),
diff= jan-jul;
if(diff> 0) return 'N';
if(diff< 0) return 'S'
return null;
})()
這篇關于有沒有一種簡單的方法可以確定用戶在哪個半球?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!