C # - Get country from latitude / longitude without API

Is it possible to use a C # library or a database of geographical coordinates updates coutries to get the name of a country from latitude and longitude Without an API , for example, the Google Maps JavaScript API?

+5
source share
3 answers

Can you do this. If you do not need more accuracy, you can use the table with the coordinates of the countries. And determine the nearest coordinate of the country. But this is bad.

A good solution is that you get a KML file with country borders. But you need a good algorithm to determine which country you are in. The algorithm for finding the position of a point and a polygon is described here . There are also answers to stackoverflow 1 , 2 , but this is for an airplane.

It is also possible to pre-calculate countries for coordinates using a small step (for example, 30 '') and determine coutry by finding the nearest pre-calculated point to your point. You can pre-enroll the country with many online services.

+1
source

You can use GeoNames and download the local database here .

So, this is an example query that you can use to get the closest record from the 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); 
+4
source

I created a github project that will get the state of a country or America based on the gps (lat; lon) point see here

if found, will return the locationInfo class with identifier and name

also available as nuget package

0
source

Source: https://habr.com/ru/post/1240998/


All Articles