2010-12-12 40 views
-1

我该怎么做this或类似this找到用户的最佳方式

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

我想用这个在网页中

避免维护IP的和它们的位置的一个庞大的数据库!

我的重点是: 服务应该是: 1.Free 2.Most准确

回答

2

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

+0

这些都有多准确?他们可以找到一个城市郊区有多深? – Pratik 2010-12-12 10:06:42

+0

Hostip.info不能正常工作。例如选中:116.72.105.74 – Pratik 2010-12-12 10:11:41

+0

它也不再可用,请查看我的服务https://ipdata.co – Jonathan 2017-09-05 17:44:37

0

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

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

+0

API密钥是否有任何免费的网络服务来检测用户的地址? – Pratik 2010-12-12 10:05:02

+0

MaxMind City数据库可在[here](http://www.maxmind.com/app/geolitecity)获得,他们在25英里半径范围内声称在美国有79%的准确率。 – cspolton 2010-12-12 10:09:16

+1

我使用http://www.fraudlabs.com的ip2location web服务。他们有免费帐户和许可证密码 – 2010-12-16 09:19:30

0

的HTML5地理位置API工作在从未浏览器(Safari浏览器,浏览器,火狐),但它需要在计算机上的某种位置服务的。如果HTML5地理位置不可用,我会首先使用该地址,然后使用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

+0

什么是Internet Explorer 7/8和Opera – Pratik 2010-12-14 05:08:02

+0

Opera yes, IE号 – adamse 2010-12-14 12:33:01

0

这个伟大的工程,据我测试了它。

获得从ipinfodb.org

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 ''; 
    } 
}