简体   繁体   中英

how to get country name from latitude and longitude

How can i get the country name from latitude and longtitude using c#? Im using the Bing.Map API

Location location12 = new Location(location.Latitude, location.Longitude);
MapLayer.SetPosition(pin, location12);
Map.Children.Add(pin);
string placeName = GetPlaceNameForCoordinates(location.Latitude, location.Longitude);   

You'll want to use a reverse geocoding API of some kind. For example:

If you're already using the Bing.Maps SDK, you should use the Map.SearchManager property to get a SearchManager , and then use the ReverseGeocodeAsync method. In particular, as noted in comments, mixing and matching which API you use to show data via other SDKs may well violate the terms and conditions of both: be careful which technologies you use within a single application. (Even though this question gives sample code using the Bing Maps SDK, I've kept the list above in order to help others who may come with a slightly different context.)

For example:

var manager = map.SearchManager;
var request = new ReverseGeocodeRequestOptions(location) {
    IncludeEntityTypeFlags = ReverseGeocodeEntityType.CountryRegion
};
var response = await manager.ReverseGeocodeAsync(
    new ReverseGeocodeRequestOptions(location) { );
// Use the response

If you decide to use the Geonames API, the data can be downloaded for offline use too.

I've created a github project with sample code that does this without api calls

see here

Very simple country name and id returned

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