What is the simplest way to get the country name from longitudes and latitudes. I have been looking on the internet and I did find an ANE but it's not free. I need a very simple way, I don't need any other features. It can be online or offline. Another API I found is Geonames API but I can't seem to find out how to use it in AS3 to just get the country name by providing the longitudes and latitudes.
Any help is greatly appreciated.
As you've mentioned in your question, you can use the findNearby Geonames's WebService which you can use to load the data ( XML or JSON ) via an URLLoader
object :
var lat:Number = 37.26801,
lng:Number = -115.80031;
var url:String = 'http://api.geonames.org/findNearby?lat=' + lat + '&lng=' + lng + '&username=demo';
var url_request:URLRequest = new URLRequest(url);
var url_loader:URLLoader = new URLLoader();
url_loader.addEventListener(Event.COMPLETE, on_DataLoad);
url_loader.load(url_request);
function on_DataLoad(e:Event): void
{
var xml:XML = new XML(e.target.data);
trace(xml.geoname.countryName); // gives : United States
}
The XML data returned by that request :
<geonames>
<geoname>
<toponymName>Groom Lake</toponymName>
<name>Groom Lake</name>
<lat>37.26801</lat>
<lng>-115.80031</lng>
<geonameId>5505124</geonameId>
<countryCode>US</countryCode>
<countryName>United States</countryName>
<fcl>H</fcl>
<fcode>LK</fcode>
<distance>0.07718</distance>
</geoname>
</geonames>
For this code, I used the demo account available to test the Geonames's WebServices, but you should create your own account to use it.
You can also use the Google Maps Geocoding API after getting a key :
url = 'https://maps.googleapis.com/maps/api/geocode/xml?latlng=' + lat + ',' + lng + '&key=YOUR_API_KEY_GOES_HERE';
then
// ...
function on_DataLoad(e:Event): void
{
var xml:XML = new XML(e.target.data);
trace(xml.result[0].address_component.(type[0] == 'country').long_name); // gives : United States
}
Where the XML content was :
<GeocodeResponse>
<status>OK</status>
<result>
<type>route</type>
<formatted_address>Unnamed Road, Nevada, United States</formatted_address>
<address_component>
<!-- ... -->
</address_component>
<address_component>
<!-- ... -->
</address_component>
<address_component>
<!-- ... -->
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<!-- ... -->
</geometry>
<place_id>
<!-- ... -->
</place_id>
</result>
<!--
...
-->
</GeocodeResponse>
Hope that can help.