10

Is it possible with a C# library or an update database about geographical coordinates coutries to get the country name from latitude and longitude Without an API like the Google Maps’ JavaScript API ?

sborfedor
  • 316
  • 1
  • 9
  • 24
k4st0r42
  • 1,174
  • 1
  • 15
  • 29

3 Answers3

5

I've created a github project that will get country or american state based on gps point (lat ; lon) see here

if found, will return a locationInfo class with Id and Name

also available as a nuget package

InquisitorJax
  • 1,062
  • 11
  • 20
3

You can use GeoNames and download a local database here.

And so, this is a sample query that you can use to obtain the nearest entry from a lat/long coordinate.

var query = new SQLiteCommand("select name,latitude,longitude,country_code,feature_code from geoloc order by ((latitude - @lat) * (latitude - @lat) + (longitude - @lng) * (longitude - @lng)) LIMIT 1, cn);
sborfedor
  • 316
  • 1
  • 9
  • 24
  • 1
    If i well understood, the database is data about cities with their coordinates and their countries. I just need to calculate the nearest city from my point (latitude, longitude) and check the country ? According to fryday, it is not the best solution. – k4st0r42 Jan 20 '16 at 10:00
  • @k4st0r42, such approach will be bad in ocens for example. It is better to use approach with country borders. – fryday Jan 20 '16 at 10:02
  • @k4st0r42 right. I've update my response with a sample query to obtain the nearest entry from a lat/long coordinate. – sborfedor Jan 20 '16 at 10:05
  • 1
    It wont work correctly. What about point with longitude 179 and the nearest city will be on -179 longitude? – fryday Jan 20 '16 at 10:11
  • @fryday It 'an approximation. Which for most of the cases works well. – sborfedor Jan 20 '16 at 10:14
  • Yes in most. Point for example https://www.google.ru/maps/@68.750916,179.5761591,9z – fryday Jan 20 '16 at 10:17
  • Do you think this formula is better ? http://stackoverflow.com/a/33166084/3767408 – k4st0r42 Jan 20 '16 at 10:18
1

You can do it. If you don't need a lot of accuracy, you can use a table with coordinates of countries. And determine nearest country coordinate. But it is bad way.

Good solution is you get a KML file of country borders. But you will need good algorithm to detemine in border of which country your point is. The algorithm for finding posiiton of point and polygon is described here. Also there are some answers on stackoverflow 1, 2, but it's for plane.

Also possible way is to precalculate countries for coordinates with some small step(e.g. 30'') and determine coutry by finding nearest precalculated point to your point. You can precalculate country with a lot of online services.

Community
  • 1
  • 1
fryday
  • 387
  • 2
  • 14