Author |
Message |
26/04/2006 20:01:02
|
Anonymous
|
I'm trying to use the reverse geocoding service here to get postal codes by lat/lng info in Javascript.
However, it never runs into the callback function because I always get the redyState as 1 (means retrieving) and I cannot get responseText. The URL I make works when I use it manually with a browser.
So what's wrong with the following code?
Your helps are highly appreciated!
var xmlhttp = null;
var RevGeocodingHtml = 'http://ws.geonames.org/findNearbyPostalCodes?lat='+lat+'&lng='+lng;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
if ( typeof xmlhttp.overrideMimeType != 'undefined') {
xmlhttp.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Perhaps your browser does not support xmlhttprequests?');
}
xmlhttp.open('GET', RevGeocodingHtml, true);
alert(xmlhttp.readyState);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// do something with the results
var myObj = eval ( xmlhttp.responseText );
} else {
// wait for the call to complete
}
};
|
|
|
27/04/2006 07:02:58
|
marc
Joined: 08/12/2005 07:39:47
Messages: 4412
Offline
|
The problem with your code lies in a browser security feature which makes it impossible to call an xml service from an other server then the one the page has been loaded from.
See this geonames faq entry for details :
http://forum.geonames.org/gforum/posts/list/11.page
Instead of calling the xml service :
http://ws.geonames.org/findNearbyPostalCodes?postalcode=6020&country=CH
you can call the JSON variant of the same service :
http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode=6020&country=CH
For the JSON format there is no security restriction on the browser.
Marc
|
|
|
|
27/04/2006 20:33:46
|
Anonymous
|
I know they offer JSON variant for geocoding, but it doesn't seem like they offer JSON for reverse geocoding.
Maybe I'm wrong?
Many thanks for your reply!
marc wrote:
The problem with your code lies in a browser security feature which makes it impossible to call an xml service from an other server then the one the page has been loaded from.
See this geonames faq entry for details :
http://forum.geonames.org/gforum/posts/list/11.page
Instead of calling the xml service :
http://ws.geonames.org/findNearbyPostalCodes?postalcode=6020&country=CH
you can call the JSON variant of the same service :
http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode=6020&country=CH
For the JSON format there is no security restriction on the browser.
Marc
|
|
|
27/04/2006 21:02:07
|
marc
Joined: 08/12/2005 07:39:47
Messages: 4412
Offline
|
Who is 'they'? Is it a company starting with 'Y'? If yes, I don't think 'they' offer reverse geocoding.
But this is no problem as you can use our service for this
Marc
|
|
|
|
27/04/2006 23:45:37
|
Anonymous
|
Marc, would you like to try the following code and see what's wrong?
Put "http://ws.geonames.org/findNearbyPostalCodesJSON?lat=45&lng=-123" directly to a brower window, I can see an output like: {"postalCodes":[{"postalCode":"97303","lat":44.992731,"placeName":"Salem","countryCode":"US","lng":-123.01672},{"postalCode":"97313","lat":44.984941,"placeName":"Salem","countryCode":"US","lng":-122.998756}]}
However, with the call of bObj.addScriptTag(), I always saw the error message: Error: Expected ';' Code: 0
I'd really appreciate your comments!
<html>
<body>
<script type="text/javascript" src="jsr_class.js"> </script>
<script type="text/javascript">
// Define the callback function
function getGeo(jsonData) {
alert('...');
}
// The web service call
var req = 'http://ws.geonames.org/findNearbyPostalCodesJSON?lat=45&lng=-123';
// Create a new request object
bObj = new JSONscriptRequest(req);
// Build the dynamic script tag
bObj.buildScriptTag();
// Add the script tag to the page
bObj.addScriptTag();
</script>
</body>
</html>
marc wrote:
Who is 'they'? It is a company starting with 'Y'? If yes, I don't think 'they' offer reverse geocoding.
But this is no problem as you can use our service for this
Marc
|
|
|
28/04/2006 17:27:44
|
marc
Joined: 08/12/2005 07:39:47
Messages: 4412
Offline
|
Anonymous wrote:
<script type="text/javascript" src="jsr_class.js"> </script>
<script type="text/javascript">
// Define the callback function
function getGeo(jsonData) {
alert('...');
}
// The web service call
var req = 'http://ws.geonames.org/findNearbyPostalCodesJSON?lat=45&lng=-123';
You have to add a parameter callback=getGeo to tell our werbservice the name of the function where you are processing the result.
http://ws.geonames.org/findNearbyPostalCodesJSON?lat=45&lng=-123&callback=getGeo
|
|
|
28/04/2006 23:06:39
|
Anonymous
|
This works like a charm, though I realized how sily I am
Thank you very much, Marc and Geonames!
marc wrote:
You have to add a parameter callback=getGeo to tell our werbservice the name of the function where you are processing the result.
http://ws.geonames.org/findNearbyPostalCodesJSON?lat=45&lng=-123&callback=getGeo
|
|
|
|