本文主要是介绍C#使用GeoLite2根据ip进行ip和地理位置的转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先下载得到GeoLite2-City.mmdb,放入项目根目录
using MaxMind.GeoIP2;
public string[] Map_ip(string ip)
{string[] loc = { "", "", "" };using (var reader = new DatabaseReader("GeoLite2-City.mmdb")){var city = reader.City(ip);loc[0] = city.Country.Name; //国名loc[1] = city.City.Name; //城市名loc[2] = city.Continent.Name; //大洲名//Console.WriteLine(city.Country.IsoCode); // 'US'//Console.WriteLine(city.Country.Name); // 'United States'//Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'//Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota'//Console.WriteLine(city.MostSpecificSubdivision.IsoCode); // 'MN'//Console.WriteLine(city.City.Name); // 'Minneapolis'//Console.WriteLine(city.Postal.Code); // '55455'//Console.WriteLine(city.Location.Latitude); // 44.9733//Console.WriteLine(city.Location.Longitude); // -93.2323}return loc;
}
这篇关于C#使用GeoLite2根据ip进行ip和地理位置的转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!