2

JavaScript 或 C# 中是否有一种方法可以通过 IP 地址查找用户的位置,而无需查询第三方的 API?

4

3 回答 3

3

您认为它将如何工作,您如何解决该位置?你有一个包含所有信息的数据库吗?没有权利。我们必须使用网络上的一些服务并获取位置。有很多简单的 API 提供这些服务。

试试这个,我主要使用这个,结果可以是 JSON 或 XML 或 CSV

freegeoip.net

http://freegeoip.net/{format}/{ip}
http://freegeoip.net/json/216.239.51.99

因此,为此,您将得到类似的结果

{
    "ip": "216.239.51.99",
    "country_code": "US",
    "country_name": "United States",
    "region_code": "CA",
    "region_name": "California",
    "city": "Mountain View",
    "zipcode": "94043",
    "latitude": 37.4192,
    "longitude": -122.0574,
    "metro_code": "807",
    "areacode": "650"
}

你可以很容易地解析这个。如果您对此有任何其他问题,请告诉我。

于 2013-07-19T06:27:59.497 回答
2

javascript或C#类有什么办法可以做到这一点吗?

不,没有。这就是人们使用 IP 地址构建整个数据库并为其提供 API 的原因。

于 2013-07-19T06:28:23.920 回答
2

There is no way that I am aware of.

To understand why you need to look at how IP Geolocation works: IP addresses don't necessarily come with the location information attached to them. To find out which country an IP address belongs to you have to do a lookup in a huge database of IP addresses and their locations.

IP Geolocation providers assemble these databases from a variety of sources. The Geolocation entry on Wikipedia gives a more detailed explanation where the information actually comes from. These providers collect information from many sources, match the information and create a database from it. This is a somewhat complicated process which is why you are usually charged to access these databases.

In theory you could create or download your own database and then query that. However, this is not really feasible. You would have to spent a lot of resources to create your own database. Downloading will not be easy as the Geolocation providers will not want to give their databases away with charging for access being their business model. Also remember that you will need to maintain your database as IP addresses are reallocated frequently.

于 2013-07-19T06:31:57.680 回答