| Author |
Message |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 08/09/2010 10:48:23
|
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
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 08/09/2010 11:44:56
|
geotree
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 |
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 09/09/2010 17:28:44
|
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!
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 12/09/2010 17:29:46
|
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
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 14/09/2010 07:35:39
|
geotree
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 |
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 18/09/2010 14:21:51
|
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!
|
|
|
 |
![[Post New]](/gforum/templates/default/images/icon_minipost_new.gif) 21/09/2010 10:58:21
|
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 |
|
|
 |
|
|