GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
States list per country?  XML
Forum Index -> General
Author Message
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!!!
russau


[Avatar]
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.
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?
russau


[Avatar]
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.
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!
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

[WWW]
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!!
russau


[Avatar]
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...
 
Forum Index -> General
Go to:   
Powered by JForum 2.1.5 © JForum Team