geolocation.js

// JavaScript Document
// geolocation.js

function getCoordinates()
{
    var options = {
        enableHighAccuracy: true,   //这个参数不影响精度
        timeout: 5000,
        maximumAge: 0
      };
      
      function success(pos) {
        var coords = pos.coords;
        console.log(coords);
        console.log(pos);
        console.log('Your current position is:');
        console.log('Latitude : ' + coords.latitude);
        console.log('Longitude: ' + coords.longitude);
        console.log('More or less ' + coords.accuracy + ' meters.');
      };
      
      function error(err) {
        console.log('ERROR(' + err.code + '): ' + err.message);
      };
      
      
    navigator.geolocation.getCurrentPosition(success,error,options);
}

function gtCooordinates()
{
    function showPostion(position)
    {
        console.log(position.coords);
    }
    navigator.geolocation.getCurrentPosition(showPostion);
}


var geoinfo=new Object();
/*
http://ifconfig.me/ip
http://ipinfo.io/ip
https://ifconfig.co/ip
*/
function getgeoinfo()
{
    var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');

    var xhr=new XMLHttpRequest;
    xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
    xhr.onreadystatechange=function()
    {
        if(xhr.readyState==4&&this.status==200)
        {
            if(xhr.response!=''&&xhr.response!=null)
            {
                try
                {
                    sessionStorage.setItem('transportbuf',xhr.response.replace(/\s*/g,""));
                    //console.log(xhr.response.replace(/\s*/g,""));
                    if(geoinfo==null)
                    {
                        geoinfo=new Object();
                    }
                    geoinfo.ipv4=sessionStorage.getItem('transportbuf');
                    localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
                    function showPostion(position)
                    {
                        geoinfo.latitude=position.coords.latitude;
                        geoinfo.longitude=position.coords.longitude;
                        localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
                    }
                    navigator.geolocation.getCurrentPosition(showPostion);
                    
                    //console.log(geoinfo);
                }
                catch(err)
                {
                    console.log(xhr.response);
                    console.log(xhr.responseText);
                    console.log(sessionStorage.getItem('transportbuf'));
                    throw err;
                }
            }
        }
    }
    xhr.withCredentials=false;
    try
    {
        xhr.send(null,true);
    }
    catch(err)
    {
        console.log(xhr.responseURL);
        console.log(err);
    }
}


function getgeoinfo(fn)
{
    var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');

    var xhr=new XMLHttpRequest;
    xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
    xhr.onreadystatechange=function()
    {
        if(xhr.readyState==4&&this.status==200)
        {
            if(xhr.response!=''&&xhr.response!=null)
            {
                try
                {
                    sessionStorage.setItem('transportbuf',xhr.response.replace(/\s*/g,""));
                    //console.log(xhr.response.replace(/\s*/g,""));
                    //console.log(sessionStorage.getItem('transportbuf'));
                    if(geoinfo==null)
                    {
                        geoinfo=new Object();
                    }
                    geoinfo.ipv4=sessionStorage.getItem('transportbuf');
                    localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
                    function showPostion(position)
                    {
                        geoinfo.latitude=position.coords.latitude;
                        geoinfo.longitude=position.coords.longitude;
                        localStorage.setItem('geoinfo',JSON.stringify(geoinfo));
                    }
                    navigator.geolocation.getCurrentPosition(showPostion);
                    
                    if(fn!=null && typeof fn == 'function')
                    {
                        //console.log('success function',geoinfo);
                        fn();
                    }
                }
                catch(err)
                {
                    console.log(xhr.response);
                    console.log(xhr.responseText);
                    console.log(sessionStorage.getItem('transportbuf'));
                    throw err;
                }
            }
        }
    }
    xhr.withCredentials=false;
    try
    {
        xhr.send(null,true);
    }
    catch(err)
    {
        console.log(xhr.responseURL);
        console.log(err);
    }
}
// JavaScript Document
// ip.js

window.onload=getpublicIp;

function getpublicIp()
{
    //xhr无法使用https 'https://ifconfig.co/ip'
    var urls=new Array('http://ifconfig.me/ip','http://ipinfo.io/ip');

    var xhr=new XMLHttpRequest;
    xhr.open('get',urls[Math.floor(Math.random()*urls.length)]);
    xhr.onreadystatechange=function()
    {
        if(xhr.readyState==4&&this.status==200)
        {
            if(xhr.response!=''&&xhr.response!=null)
            {
                try
                {
                    if(xhr.responseText!=null && xhr.responseText!='')
                        localStorage.setItem('ip',xhr.responseText);
                }
                catch(err)
                {
                    console.log(xhr.response);
                    throw err;
                }
            }
        }
    }
    xhr.withCredentials=false;
    try
    {
        xhr.send(null,true);
    }
    catch(err)
    {
        console.log(xhr.responseURL);
        console.log(err);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Risehuxyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值