如何从 IP 列表中找到最近的 IP?

How to find the closest IP from a list of IPs?

我想知道这是否可行

我有一个给定的 IP,例子:191.184.222.43 (public ipv4)

我还有一个其他 IP 地址的 Loong 列表,例如:

[ ip1, ip2, ip3, ip4 ...等等]

现在我必须找出列表中哪个 IP 最接近(物理上)我的 给定 IP (191.184.222.43 )

这可能吗?还是我的想法不一样

有很多API可以做到这一点的例子在https://ipstack.com/结束了。它基于 IP 进行地理定位,通过一些解析,您可以轻松完成此操作!

示例代码:

// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';

// get the API result via jQuery.ajax
$.ajax({
    url: 'https://api.ipstack.com/' + ip + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // output the "capital" object inside "location"
        alert(json.location.capital);

    }
});

示例输出 (JSON):

[
  {
    "ip": "134.201.250.155",
    "type": "ipv4",
    "continent_code": "NA",
    "continent_name": "North America",
    "country_code": "US",
    "country_name": "United States",
    "region_code": "CA",
    "region_name": "California",
    "city": "Los Angeles",
    "zip": "90013",
    "latitude": 34.0453,
    "longitude": -118.2413,
    "location": { ... },
    "time_zone": { ... },
    "currency": { ... },
    "connection": { ... },
  }
]