Retrive Lat Long from ZipCode

203 Views Asked by At

Is their is a way to get Lat Long from MaxmindDb through ZipCode rather than Ipaddress. As i cannot find any method to fetch details through zipcode. I don't want to use 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();              
            }

But I want to use UserZipCode instead of UserIPAddress

1

There are 1 best solutions below

0
scytale On

From the example in your question I assume you want the look-up table on your own server rather than use any API.

One of the Maxmind Libraries on Github might include this functionality. However Maxmind City DB actually uses Zip Code data from geonames.org and a simpler solution might be to upload the GeoNames "look-up" table Zip for US to your server.

It contains a tab delimited file (see bottom of link for full description) and you would just have to write code to find the record for by zip code and then read the lat and long fields from it. For improved speed you could import the file to a DBMS e.g. mySQL (google for instructions) with zip code as key/indexed.

I've no idea as to preciseness and accuracy of Geonames data.