30

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

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

谢谢

4

7 回答 7

40

要在 jQuery 中获取客户端 IP 地址和国家/地区名称:

$.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>

于 2015-09-24T08:16:07.580 回答
24

您可以使用支持 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/

于 2013-07-23T09:22:08.490 回答
13

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

然后,您可以对位置进行反向地理编码以获取地址,除了 Google 的 API 之外,还有几种免费的反向地理编码服务。

但是,如果您不需要准确的位置,并且如果您希望所有用户都利用该功能(无论浏览器如何),并且如果您不想询问他们是否允许您的网站拥有他们的位置,我建议使用您用户的 IP 来获取位置

于 2013-01-05T23:48:14.523 回答
9

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

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

此答案使用非常有限的“测试”API 密钥,仅用于测试几个调用。注册您自己的免费 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/上的小提琴

于 2017-11-05T18:43:15.040 回答
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>
于 2014-12-19T12:28:47.497 回答
1

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

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

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

在您的服务器上制作一个 php 脚本,它将只返回 IP 地址。由于 jquery 是在本地处理的,所以当与服务器建立连接时,客户端 IP 地址将被泄露。建立所需的连接需要一些额外的时间,但很快 IP 地址将在 jQuery 中可用。

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

于 2013-01-11T12:13:40.753 回答
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
  }
}
于 2017-08-06T20:20:10.560 回答