【问题标题】:Best way of locating users [closed]定位用户的最佳方式[关闭]
【发布时间】:2010-12-12 09:54:37
【问题描述】:

我该怎么做this 或喜欢this?

有没有免费的网络服务来检测用户的位置?

我想在网页中使用它

避免维护庞大的 IP 及其位置数据库!

我的优先事项是: 服务应该是: 1.免费 2.最准确

【问题讨论】:

    标签: javascript asp.net web-services geolocation geoip


    【解决方案1】:

    来自http://blog.programmableweb.com/2009/03/31/3-free-ways-to-geolocate-by-ip/我收到了

    Hostip.info 是一个社区支持的 IP 映射数据库。它的 REST API 很容易集成到服务器端代码中,并有多种输出类型选项。查看我们的 hostip.info API 配置文件,您可以在其中查看已经使用此 API 的混搭。

    MaxMind-Geo Lite 是一种不同类型的 API。它的免费版本不是调用 Web 服务,而是作为二进制文件分发。有用于访问 IP 数据的常用编程语言的开源库。

    另见http://www.eggheadcafe.com/articles/20051109.asp

    类似问题:https://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service

    【讨论】:

    • 所有这些准确度如何?他们能在多深的地方找到城市郊区?
    • Hostip.info 不起作用。例如检查这个:116.72.105.74
    【解决方案2】:

    我使用GeoIP Country Lite。他们每个月都会发布数据库更新,并声称准确率为 99.5%。使用起来很简单,因为它们还提供库代码来查询数据库。

    如果您需要比国家/地区更高的分辨率,MaxMind 还提供基于城市的查找。

    【讨论】:

    • 有没有免费的网络服务来检测用户的位置?
    • MaxMind City 数据库在here 可用,他们声称在美国 25 英里半径内的准确度为 79%。
    • 我正在使用来自fraudlabs.com 的 ip2location Web 服务。他们有免费帐户和许可证密钥
    【解决方案3】:

    HTML5 Geolocation API 从不适用于浏览器(Safari、Chrome、Firefox),尽管它需要计算机上的某种定位服务。如果 HTML5 Geolocation 不可用,我会先使用它,然后通过 IP 回退到位置。示例代码:

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function (pos) {
        console.log([pos.latitude, pos.longitude]);
      }, function (err) { 
        // Do something else, IP location based?
      });
    } else {
      // Do something else, IP location based?
    }
    

    要查找用户的国家、城市甚至地址,您可以使用Google Maps Geocoding API

    【讨论】:

    • Internet Explorer 7/8 和 Opera 的情况
    【解决方案4】:

    就我的测试而言,这很有效。

    从ipinfodb.org获取一个api密钥

    var Geolocation = new geolocate(false, true);
    Geolocation.checkcookie(function() {
        alert('Visitor latitude code : ' + Geolocation.getField('Latitude'));
        alert('Visitor Longitude code : ' + Geolocation.getField('Longitude'));
    });
    
    function geolocate(timezone, cityPrecision) {
        alert("Using IPInfoDB");
        var key = 'your api code';
        var api = (cityPrecision) ? "ip_query.php" : "ip_query_country.php";
        var domain = 'api.ipinfodb.com';
        var version = 'v2';
        var url = "http://" + domain + "/" + version + "/" + api + "?key=" + key + "&output=json" + ((timezone) ? "&timezone=true" : "&timezone=false" ) + "&callback=?";
        var geodata;
        var JSON = JSON || {};
    
        // implement JSON.stringify serialization
        JSON.stringify = JSON.stringify || function (obj) {
            var t = typeof (obj);
            if (t != "object" || obj === null) {
                // simple data type
                if (t == "string") obj = '"'+obj+'"';
                return String(obj);
            } 
            else {
                // recurse array or object
                var n, v, json = [], arr = (obj && obj.constructor == Array);
                for (n in obj) {
                    v = obj[n]; t = typeof(v);
                    if (t == "string") v = '"'+v+'"';
                    else if (t == "object" && v !== null) v = JSON.stringify(v);
                    json.push((arr ? "" : '"' + n + '":') + String(v));
                }
                return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
            }
        };
    
        // implement JSON.parse de-serialization
        JSON.parse = JSON.parse || function (str) {
            if (str === "") str = '""';
            eval("var p=" + str + ";");
            return p;
        };
    
        // Check if cookie already exist. If not, query IPInfoDB
        this.checkcookie = function(callback) {
            geolocationCookie = getCookie('geolocation');
            if (!geolocationCookie) {
                getGeolocation(callback);
            } 
            else {
                geodata = JSON.parse(geolocationCookie);
                callback();
            }
        }
    
        // Return a geolocation field
        this.getField = function(field) {
            try {
                return geodata[field];
            } catch(err) {}
        }
    
        // Request to IPInfoDB
        function getGeolocation(callback) {
            try {
                $.getJSON(url,
                        function(data){
                    if (data['Status'] == 'OK') {
                        geodata = data;
                        JSONString = JSON.stringify(geodata);
                        setCookie('geolocation', JSONString, 365);
                        callback();
                    }
                });
            } catch(err) {}
        }
    
        // Set the cookie
        function setCookie(c_name, value, expire) {
            var exdate=new Date();
            exdate.setDate(exdate.getDate()+expire);
            document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
        }
    
        // Get the cookie content
        function getCookie(c_name) {
            if (document.cookie.length > 0 ) {
                c_start=document.cookie.indexOf(c_name + "=");
                if (c_start != -1){
                    c_start=c_start + c_name.length+1;
                    c_end=document.cookie.indexOf(";",c_start);
                    if (c_end == -1) {
                        c_end=document.cookie.length;
                    }
                    return unescape(document.cookie.substring(c_start,c_end));
                }
            }
            return '';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多