| Author |
Message |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 17/02/2010 20:20:12
|
rshelby
Joined: 17/02/2010 20:06:53
Messages: 7
Offline
|
Hi! First off, this service is amazing!
I was wondering if there was a way with JavaScript to send geonames.org a country iso and have it send back a list of subdivisions?
Thanks!!!
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 17/02/2010 23:22:14
|
russau
Joined: 26/01/2009 12:44:48
Messages: 14
Offline
|
Have a look at this JSONP service:
http://ws.geonames.org/childrenJSON?geonameId=3175395&callback=qwer
More on JSONP: http://en.wikipedia.org/wiki/JSONP#JSONP
This allows the broswer to directly contact the geonames webservice.
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 00:48:41
|
rshelby
Joined: 17/02/2010 20:06:53
Messages: 7
Offline
|
I saw that, but you need the geonameID for that right? You can't just use "&country=US", can you?
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 00:57:31
|
russau
Joined: 26/01/2009 12:44:48
Messages: 14
Offline
|
Here's something I hacked up with jsonp and jquery:
http://tinisles.com/samples/geonames_jsonp/
The country info file will give you all the country / geonameid mappings.
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 01:30:52
|
rshelby
Joined: 17/02/2010 20:06:53
Messages: 7
Offline
|
Thanks!
And I just realized why I was confused. I forgot that my local import of countries was from a different (lesser than) source!
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 07:04:39
|
marc
Joined: 08/12/2005 07:39:47
Messages: 4501
Offline
|
You can use the search service if you don't have the geonameId:
view-source:http://ws.geonames.org/searchJSON?formatted=true&q=&maxRows=100&country=US&featureCode=ADM1
|
 |
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 16:23:38
|
rshelby
Joined: 17/02/2010 20:06:53
Messages: 7
Offline
|
Awesome! One last quick question.
My code, with that URL seems to come back with all regions, not just the regions in the country I passed to geonames.
Code:
function get_states(country_element,state_element)
{
countryElementIDHash = "#" + country_element;
stateElementIDHash = "#" + state_element;
$(stateElementIDHash).empty();
var geonameId = $(countryElementIDHash).val();
alert(geonameId);
$.getJSON("http://ws.geonames.org/searchJSON?formatted=true&q=&maxRows=100&featureCode=ADM1",
{ 'country=': $(countryElementIDHash).val() },
function(data) {
if (data.geonames == null) alert('No regions');
$.each(data.geonames, function(i,item){
$(stateElementIDHash).append($('<option>' + item.name + '</option>'));
});
}
);
}
When I look at firebug, it does pass the correct "country=". very strange!!
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/02/2010 22:43:38
|
russau
Joined: 26/01/2009 12:44:48
Messages: 14
Offline
|
Ah.. you don't need the '=' after country; e.g.
{ 'country': $(countryElementIDHash).val() },
Also I thought you need to include the "&callback=?" in the URL? But you are seeing a result in firebug, so maybe not...
|
|
|
 |
|
|