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 ?
-
1How much accurace do you need? – fryday Jan 20 '16 at 09:29
-
2Pure C#? you cannot.Possible duplicate of [how to get country name from latitude and longitude](http://stackoverflow.com/questions/25866020/how-to-get-country-name-from-latitude-and-longitude) – MaYaN Jan 20 '16 at 09:31
-
1@MaYaN, possible with good database and smart algorithm. – fryday Jan 20 '16 at 09:39
-
@k4st0r42 Yes it is possible but you have to start implementing it first and then we can help you when you stuck. – Oluwafemi Jan 20 '16 at 09:41
-
1@fryday That would not be Pure C#, it would be C# + Database :-) – MaYaN Jan 20 '16 at 13:15
-
@MaYaN, OP said 'Is it possible with a C# library or an update database' – fryday Jan 20 '16 at 13:50
3 Answers
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

- 1,062
- 11
- 20
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);

- 316
- 1
- 9
- 24
-
1If 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
-
1It 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
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.