简体   繁体   中英

C# - Get Country from latitude/longitude without API

是否可以使用C#库或有关地理坐标的更新数据库来获取纬度和经度的国家/地区名称而不使用 Google Maps的JavaScript API等API?

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);

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

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(eg 30'') and determine coutry by finding nearest precalculated point to your point. You can precalculate country with a lot of online services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM