使用jQuery / JS(没有谷歌地理定位API)查找用户位置?

时间:2013-01-05 23:39:37

标签: javascript jquery unobtrusive-javascript

有没有办法只使用jQuery在网站上找到客户位置。例如,如果用户访问我的网站,我怎么能找到他们的大概位置只是使用jQuery。例如,如果用户来自旧金山CA,则会有一些类型标识符让我知道用户来自旧金山CA.我真的不需要他们的确切位置只是县或一般的原产地。

编辑: 如何生成http://flourishworks-webutils.appspot.com/req上的信息?

由于

7 个答案:

答案 0 :(得分:40)

获取客户端IP地址& jQuery中的国家/地区名称:

$.getJSON("http://freegeoip.net/json/", function(data) {
    var country_code = data.country_code;
    var country = data.country_name;
    var ip = data.ip;
    var time_zone = data.time_zone;
    var latitude = data.latitude;
    var longitude = data.longitude;

    alert("Country Code: " + country_code);
    alert("Country Name: " + country);
    alert("IP: " + ip); 
    alert("Time Zone: " + time_zone);
    alert("Latitude: " + latitude);
    alert("Longitude: " + longitude);   
});



$.get("http://freegeoip.net/json/", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region_name);
    $("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Client side IP geolocation using <a href="http://freegeoip.net">freegeoip.net</a></h3>

<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
&#13;
&#13;
&#13;

答案 1 :(得分:23)

您可以使用支持JSONP的Web服务,例如我的服务http://ipinfo.io。它将为您提供客户端的IP地址,主机名,地理位置信息和网络所有者。以下是使用jQuery实现它的一个例子:

$.get("http://ipinfo.io", function(response) {
    $("#ip").html(response.ip);
    $("#address").html(response.city + ", " + response.region);
}, "jsonp");

这是一个更详细的JSFiddle示例,它还打印出完整的响应信息,因此您可以看到所有可用的详细信息:http://jsfiddle.net/zK5FN/2/

答案 2 :(得分:13)

HTML5 Geolocation API允许您通过一些JavaScript获取用户的纬度/经度(如果浏览器兼容,并且用户允许访问他/她的位置)。

然后,您可以reverse-geocode获取地址的位置,除Google的API之外还有多种free reverse-geocoding服务。

但是,如果您不需要准确的位置,并且您希望所有用户都能利用该功能(无论是否使用浏览器),并且您不想询问他们是否允许您的网站使用有他们的位置,我建议use your user's IP to get the location

答案 3 :(得分:7)

我创建了ipdata.co API,为此提供了一个可靠的解决方案,请参阅下面的小提琴。它返回许多有用的数据点,例如 - 位置:国家(名称和代码),地区,城市等,电子商务 - 货币代码,货币符号,时区,移动运营商数据,它检测IP地址是代理还是Tor用户

Ipdata有10个全局端点,每个端点每秒能够处理> 10,000个请求。

  

这个答案使用的“测试”API密钥非常有限,仅用于测试几个调用。 Signup用于您自己的免费API密钥,每天最多可获得1500个请求以进行开发。

$.get("https://api.ipdata.co?api-key=test", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#city").html(response.city + ", " + response.region);
    $("#response").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1><a href="https://ipdata.co">ipdata.co</a> - IP geolocation API</h1>

<div id="ip"></div>
<div id="city"></div>
<pre id="response"></pre>

https://jsfiddle.net/ipdata/6wtf0q4g/922/

的小提琴

答案 4 :(得分:3)

**jQuery(document).ready(function($) {
    jQuery.getScript('http://www.geoplugin.net/javascript.gp', function()
{
    var country = geoplugin_countryName();
    var zone = geoplugin_region();
    var district = geoplugin_city();
    console.log("Your location is: " + country + ", " + zone + ", " + district);
});
});
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
    alert("Your location is: " + geoplugin_countryName() + ", " + geoplugin_region() + ", " + geoplugin_city());
});
</script>
/*--------------------------------detailed function list not necessarily to be included---------
function geoplugin_city() { return 'Dobroyd Point';}
function geoplugin_region() { return 'New South Wales';}
function geoplugin_regionCode() { return '02';}
function geoplugin_regionName() { return 'New South Wales';}
function geoplugin_areaCode() { return '0';}
function geoplugin_dmaCode() { return '0';}
function geoplugin_countryCode() { return 'AU';}
function geoplugin_countryName() { return 'Australia';}
function geoplugin_continentCode() { return 'OC';}
function geoplugin_latitude() { return '-33.873600';}
function geoplugin_longitude() { return '151.144699';}
function geoplugin_currencyCode() { return 'AUD';}
function geoplugin_currencySymbol() { return '&amp;#36;';}
function geoplugin_currencyConverter(amt, symbol) {
    if (!amt) { return false; }
    var converted = amt * 0.9587170632;
    if (converted &lt;0) { return false; }
    if (symbol === false) { return Math.round(converted * 100)/100; }
    else { return '&amp;#36;'+(Math.round(converted * 100)/100);}
    return false;
} 
*/
//----------------example-----------------------//
<html>
 <head>
  <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>
 <body>
  <script language="Javascript">
    document.write("Welcome to our visitors from "+geoplugin_city()+", "+geoplugin_countryName());
  </script>
 </body>
</html>

答案 5 :(得分:1)

任何客户端选项都不是一个好主意,因为它们是由计算机基于周围的无线网络提供的。我敢打赌,90%的桌面用户没有激活此功能。当您访问想要您所在位置的网站时,您必须单击按钮才能同意。如果他们刚进入您的网站,那么他们想先知道您需要他们的位置。

一个好方法是向他们展示一个页面,告诉他们你为什么需要他们的位置,并且你永远不会将它用于除指定目的以外的任何其他目的,并且该位置将不会保存在您的数据库中。

如果存在运行服务器端的脚本,则存在从客户端到服务器的连接。在这种情况下,服务器必须知道IP地址。有一个技巧可以做到先获取IP地址。

在您的服务器上创建一个PHP脚本,它只返回IP地址。由于jquery是在本地处理的,因此当与服务器建立连接时,将公开客户端IP地址。这需要一些额外的时间来建立所需的连接,但很快IP地址将在jQuery中可用。

然后,您可以通过jquery调用外部API,该API将为您提供该特定IP地址的信息。您可以购买IP数据库并将其安装在您的网络服务器上,这样您就可以调用它,或者使用其他API。您可以查看http://www.ipgp.net/ip-address-geolocation-api/

答案 6 :(得分:1)

使用https://ip-api.io地理位置API可以实现这一点。它提供国家,城市,邮政编码,坐标,网络,ASN,时区。例如,使用jQuery:

$(document).ready( function() {
    $.getJSON("http://ip-api.io/api/json",
        function(data){
            console.log(data);
        }
    );
});

同样https://ip-api.io检查TOR,公共代理和垃圾邮件发送者数据库,并提供此信息。

回复示例:

{
  "ip": "182.35.213.213",
  "country_code": "US",
  "country_name": "United States",
  "region_code": "CA",
  "region_name": "California",
  "city": "San Francisco",
  "zip_code": "94107",
  "time_zone": "America/Los_Angeles",
  "latitude": 32.7697,
  "longitude": -102.3933,
  "suspicious_factors": {
    "is_proxy": true,
    "is_tor_node": true,
    "is_spam": true,
    "is_suspicious": true // true if any of other fields (is_proxy, is_tor_node, is_spam) is true
  }
}