GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Help with JSON please  XML
Forum Index -> General
Author Message
RobertoS



Joined: 08/09/2010 10:45:25
Messages: 4
Offline

Hello

I am trying to obtain the toponymName from http://ws.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9.

Here is my code:

Code:
 $.getJSON("http://ws.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9", function(data){
 alert(data.toponymName);
 });
 


What am I doing wrong?

Thank you.

roberto
geotree


[Avatar]
Joined: 23/07/2007 18:28:40
Messages: 138
Location: France
Offline

It is better to use $.ajax() with dataType:'jsonp'

Have a look to geotree.html :
view-source:http://geotree.geonames.org/geotree.html

Christophe
geotree.geonames.org
geotree.geonames.org/geotree.html
[WWW]
RobertoS



Joined: 08/09/2010 10:45:25
Messages: 4
Offline

OK, this is the best I can do after spending the afternoon reverse-engineering the sample script:

Code:
 var geonames;
 function getLocation(jData)
 {
   if (jData == null) {
     return;
   }
   geonames = jData.geonames;
 	var placeInput = document.getElementById("placeInput").value = jData.geonames.toponymName;
 }
 
 
 function placeLookup()
 {
   request = 'http://ws.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9&callback=getLocation';
   aObj = new JSONscriptRequest(request);
   aObj.buildScriptTag();
   aObj.addScriptTag();
 }


What am I missing?

Thanks!
RobertoS



Joined: 08/09/2010 10:45:25
Messages: 4
Offline

$(function() {
$.ajax({
url: 'http://ws.geonames.org/findNearbyPlaceNameJSON',
dataType:'jsonp',
data: {
lat: '47.3',
lng: '9',
},
success:function(response) {
alert(response.geonames.length);
alert(response.geonames[0]);

}
});
});


OK, so this is giving me an array of length 1 (good). But how do I retrieve the "toponymName"?

Thanks for your help.

Roberto
geotree


[Avatar]
Joined: 23/07/2007 18:28:40
Messages: 138
Location: France
Offline

This is the power of json :
javascript data from 'response' array have exactly the same name as in the json demo output :
view-source:http://ws.geonames.org/findNearbyPlaceNameJSON?formatted=true&lat=47.3&lng=9&style=full

Code:
 success: function(response) {
 	// ws returns an error message
 	if (response.status) {
 		alert(response.status.message+response.status.value);
 	}
 	// ws returns an array of data
 	if (response.geonames && response.geonames.length) {
 		$.each(response.geonames, function() {
 			alert(this.fcode);
 			alert(this.toponymName);
 			etc...
 		}
 	}
 } 
 

Once again, have a look to geotree.html :
view-source:http://geotree.geonames.org/geotree.html

Christophe
geotree.geonames.org
geotree.geonames.org/geotree.html
[WWW]
RobertoS



Joined: 08/09/2010 10:45:25
Messages: 4
Offline

Thank you. I managed to obtain the data I was looking for.

I am testing your commercial service now!
taohui



Joined: 21/09/2010 10:53:48
Messages: 3
Location: http://forum.kaiyuan.de/
Offline

here is a good example:http://jqueryui.com/demos/autocomplete/#remote-jsonp

kaiyuan
[WWW] [MSN]
 
Forum Index -> General
Go to:   
Powered by JForum 2.1.5 © JForum Team