首页 > 解决方案 > 从 ZipCode 中检索 Lat Long

问题描述

他们是通过 ZipCode 而不是 Ipaddress从MaxmindDb获取 Lat Long 的一种方式。因为我找不到任何通过邮政编码获取详细信息的方法。我不想使用 Google Maps Api。

using (var reader = new DatabaseReader(System.Web.Hosting.HostingEnvironment.MapPath("~/GeoLite2-City.mmdb")))
            {
                var city = reader.City(userIpAddress);
                string cityiso = string.Empty;
                if (city != null && !string.IsNullOrEmpty(city.City.Name) && !string.IsNullOrEmpty(city.MostSpecificSubdivision.IsoCode))
                {
                    cityiso = string.Join(", ", city.City.Name, city.MostSpecificSubdivision.IsoCode);
                }
                locationProperties.Location = cityiso;
                locationProperties.Latitude = city.Location.Latitude.ToString();
                locationProperties.Longitude = city.Location.Longitude.ToString();              
            }

但我想使用 UserZipCode 而不是 UserIPAddress

标签: asp.net-mvcgeoipmaxmindgeoip2geolite2

解决方案


从您问题中的示例中,我假设您希望在自己的服务器上使用查找表,而不是使用任何 API。

Github上的Maxmind 库之一 可能包含此功能。然而,Maxmind City DB 实际上使用来自geonames.org的邮政编码数据,一个更简单的解决方案可能是将美国的GeoNames“查找”表Zip 上传到您的服务器。

它包含一个制表符分隔的文件(有关完整描述,请参见链接底部),您只需编写代码以通过邮政编码查找记录,然后从中读取经纬度字段。为了提高速度,您可以将文件导入 DBMS,例如 mySQL(google for instructions),并使用邮政编码作为键/索引。

我不知道 Geonames 数据的准确性和准确性。


推荐阅读