有没有简单的方法来确定用户在哪个半球? [英] Is there an easy way to determine which hemisphere a user is in?

查看:67
本文介绍了有没有简单的方法来确定用户在哪个半球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含季节性内容的项目,我们正在考虑确定用户的位置,以确定适合他们的季节。这样做的显而易见的方法是对其IP进行地理定位,然后获取纬度。 > 0是北半球,< 0是南方的。



我很乐意这样做 - 尽管把IP定位到一个确切位置看起来有些浪费,只是为了确定它们的哪一半但是我想我会把它扔到那里,以防任何人有任何可能缩短流程的窍门。



请求头文件,可以在客户端使用JS提取的东西,这很容易得到 - 我只是认为它没有任何帮助。

解决方案

如果客户的日历中存在夏令时,我会先检查客户的时钟 -

您可以如果没有dst信息,可以使用地理定位,

如果没有dst信息,

  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;
})( )


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.

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.

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.

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;
})()

这篇关于有没有简单的方法来确定用户在哪个半球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆