使用 Javascript 到位置的 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4937517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:51:51  来源:igfitidea点击:

IP to Location using Javascript

javascriptjqueryip

提问by SOF User

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

I can get User IP by this code...

我可以通过此代码获取用户IP...

I want to find location of this IP. How can I?

我想找到这个IP的位置。我怎样才能?

回答by

You can submit the IP you receive to an online geolocation service, such as http://www.geoplugin.net/json.gp?ip=<your ip here>&jsoncallback=<suitable javascript function in your source>, then including the source it returns which will run the function you specify in jsoncallbackwith the geolocation information.

您可以将收到的 IP 提交给在线地理定位服务,例如http://www.geoplugin.net/json.gp?ip=<your ip here>&jsoncallback=<suitable javascript function in your source>,然后包括它返回的来源,该来源将运行您在jsoncallback地理定位信息中指定的功能。

Alternatively, you may want to look into HTML5's geolocation features -- you can see a demo of it in action here. The advantage of this is that you do not need to make requests to foreign servers, but it may not work on browsers that do not support HTML5.

或者,您可能想要查看 HTML5 的地理定位功能——您可以在此处查看它的实际演示。这样做的好处是您不需要向外部服务器发出请求,但它可能不适用于不支持 HTML5 的浏览器。

回答by Alex Czarto

A free open source community run geolocation ip service that runs on the MaxMind database is available here: https://ipstack.com/

在 MaxMind 数据库上运行的免费开源社区运行地理定位 ip 服务可在此处获得:https://ipstack.com/

Example

例子

https://api.ipstack.com/160.39.144.19

Limitation

局限性

10,000 queries per month

每月 10,000 次查询

回答by Ken

It's quite easy with an APIthat maps IP address to location. Run the snippet to get city & country for the IP in the input box.

使用将 IP 地址映射到位置的API非常容易。运行代码段以获取输入框中 IP 的城市和国家/地区。

$('.send').on('click', function(){

  $.getJSON('https://ipapi.co/'+$('.ip').val()+'/json', function(data){
      $('.city').text(data.city);
      $('.country').text(data.country);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input class="ip" value="8.8.8.8">
<button class="send">Go</button>
<br><br>
<span class="city"></span>, 
<span class="country"></span>

回答by Chong Lip Phang

I wish to point out that if you use http://freegeoip.net/, you don't need to supply to it the IP address of the client's location. Just try these:

我想指出的是,如果您使用http://freegeoip.net/,则无需向其提供客户端位置的 IP 地址。试试这些:

1) http://freegeoip.net/xml/

1) http://freegeoip.net/xml/

2) http://freegeoip.net/json/

2) http://freegeoip.net/json/

3) http://freegeoip.net/csv/

3) http://freegeoip.net/csv/

However, I am unable to retrieve the information with AJAX calls, probably because of some cross-origin policy. Apparently they have not allowed public access to their system.

但是,我无法通过 AJAX 调用检索信息,可能是因为某些跨域策略。显然,他们不允许公众访问他们的系统。

回答by talsibony

Just in case you were not able to accomplish the above code, here is a simple way of using it with jquery:

以防万一你不能完成上面的代码,这里有一个简单的方法来使用它和 jquery:

$.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?",
    function (data) {
        for (var i in data) {
            document.write('data["i"] = ' + i + '<br/>');
        }
    }
);

回答by Claudiu

A better way is to skip the "middle man" (ip)

更好的方法是跳过“中间人”(ip)

jQuery.get("http://ipinfo.io", function(response) {
    console.log(response.city);
}, "jsonp");

This gives you the IP, the city, the country, etc

这给你IP城市国家

回答by Sean Keane

    $.getJSON('//freegeoip.net/json/?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

回答by Jonathan

A rather inexpensive option would be to use the ipdata.coAPI, it's free upto 1500 requests a day.

一个相当便宜的选择是使用ipdata.coAPI,它每天最多可以免费 1500 个请求。

This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signupfor your own Free API Key and get up to 1500 requests daily for development.

这个答案使用了一个非常有限的“测试”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");
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

View the Fiddle at https://jsfiddle.net/ipdata/6wtf0q4g/922/

https://jsfiddle.net/ipdata/6wtf0q4g/922/查看小提琴

回答by user396404

Either one of the following links should take care of this:

以下任一链接都应处理此问题:

http://ipinfodb.com/ip_location_api_json.php

http://ipinfodb.com/ip_location_api_json.php

http://www.adam-mcfarland.net/2009/11/19/simple-ip-geolocation-using-javascript-and-the-google-ajax-search-api/

http://www.adam-mcfarland.net/2009/11/19/simple-ip-geolocation-using-javascript-and-the-google-ajax-search-api/

Those links have tutorials for getting a users location through Javascript. However, they do so through an API to an external data service. If you have an extremely high traffic site, you might want to hosting the data yourself (or getting a premium api service). To host everything yourself, you will have to host a database with IP Geolocation and use ajax to feed the users location into Javascript. If this is the approach you want to take, you can get a free database of IP information below:

这些链接有通过 Javascript 获取用户位置的教程。但是,它们是通过 API 到外部数据服务来实现的。如果您有一个流量极高的站点,您可能希望自己托管数据(或获得高级 api 服务)。要自己托管所有内容,您必须使用 IP 地理定位托管一个数据库,并使用 ajax 将用户位置提供给 Javascript。如果这是您想要采用的方法,您可以在下面获得一个免费的 IP 信息数据库:

http://www.ipinfodb.com/ip_database.php

http://www.ipinfodb.com/ip_database.php

Please note that this method entails having to periodically update the database to stay accurate in tracing ips to locations.

请注意,此方法需要定期更新数据库以保持准确跟踪 ip 到位置。

回答by Rafay

you can use ipinfodbafter getting your api key you can query for a location against a specific ip like this http://api.ipinfodb.com/v2/ip_query.php?key=" + apiKey + "&ip=" + ip + "&output=xmlyou can then then extract the location from the xml response

您可以在获取 api 密钥后使用ipinfodb您可以像这样针对特定 ip 查询位置,http://api.ipinfodb.com/v2/ip_query.php?key=" + apiKey + "&ip=" + ip + "&output=xml然后您可以从 xml 响应中提取位置