GeeCON Prague 2014*
The moose likes Web Services and the fly likes Google Geocoder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "Google Geocoder" Watch "Google Geocoder" New topic
Author

Google Geocoder

Mark Varley
Greenhorn

Joined: May 20, 2003
Posts: 15
Hi all - I am trying to access the Google geocoding service using the web services feature in NetBeans 6.1. I have succesfully connnected to the service and am able to get a response back from Google. However, I am having trouble getting to the individual data components of the response. My code is as follows:

String strAddress = "Buckingham Palace Road, London, SW1V, GB";
String output = "kml";
RestResponse result = GoogleGeocodingService.geocode(strAddress, output);
google.geocodingservice.geocoder.Kml resultObj = result.getDataAsObject(google.geocodingservice.geocoder.Kml.class);
System.out.println("The SaasService returned: " + result.getDataAsString());
if (resultObj != null && resultObj.getResponse() !=null && resultObj.getResponse().getPlacemark() != null) {
String addressOut = resultObj.getResponse().getPlacemark().getAddress().toString();
String coordinates = resultObj.getResponse().getPlacemark().getPoint().getCoordinates().toString();
System.out.print("Address: " + addressOut + "; ");
System.out.println("Co-ordinates: " + coordinates + "; ");
}

My response is:

The SaasService returned: <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>Buckingham Palace Road, London, SW1V, GB</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>Buckingham Palace Rd, London, Greater London, SW1V 1, UK</address><AddressDetails Accuracy="6" xmlns="urn asis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>GB</CountryNameCode><AdministrativeArea><AdministrativeAreaName>England</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Greater London</SubAdministrativeAreaName><Locality><LocalityName>London</LocalityName><Thoroughfare><ThoroughfareName>Buckingham Palace Rd</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>SW1V 1</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>-0.145395,51.495832,0</coordinates></Point></Placemark></Response></kml>
Address: [address: null]; Co-ordinates: [coordinates: null];

As you can see, whilst the response is successful the data is not being correctly rendered to the google.geocodingservice.geocoder.Kml class.

Does anyone have any idea what I am doing wrong or have any suggestions how I can retrieve the information I need (Lat, Long, Accuracy)?

Thanks in advance,

mark
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
Assuming you can't get the provided API calls to work, you could use any generic XML API (e.g. DOM or XPath) to access individual elements of the response.


Ping & DNS - my free Android networking tools app
Mark Varley
Greenhorn

Joined: May 20, 2003
Posts: 15
Hi - thanks for the tip. I tried reading the response using XPath (I don't have much experience with XML parsers) and am having some problems. My code is as follows:

String strAddress = "Buckingham Palace Road, London, SW1V, GB";
String output = "kml";
RestResponse result = GoogleGeocodingService.geocode(strAddress, output);
InputStream is = new ByteArrayInputStream(result.getDataAsByteArray());
InputSource isrc = new InputSource(is);
DOMParser parser = new DOMParser();
parser.parse(isrc);
Document document = parser.getDocument();
document.getDocumentElement().normalize();
System.out.println(coordinates);

Basically, the document is loaded and works fine (I have been able to print the tree view with aa recursive function) but XPath doesn't find the co-ordinates.

I have searched the forums and they suggest that the problem is to do with namespaces. I tried modifying my code s follows but I get an error. The code is:

The error is:

Exception in thread "main" java.lang.Exception: Transaction failed
at com.compareanddive.test.BatchGeocoder.main(BatchGeocoder.java:108)
Caused by: org.apache.xpath.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: kml

If anyone can help me either fix this or the underlying problem with the Google web service mapping I would be very grateful.

Thanks,

mark
Mark Varley
Greenhorn

Joined: May 20, 2003
Posts: 15
PLEASE IGNORE THE ABOVE POST - THE JAVA RANCH FORUM STOPPED ME POSTING MY CODE PROPERLY - PLEASE FIN BELOW THE CODE AMENDED WITH EVVAL INSTEAD OF EVAL

Hi - thanks for the tip. I tried reading the response using XPath (I don't have much experience with XML parsers) and am having some problems. My code is as follows:

String strAddress = "Buckingham Palace Road, London, SW1V, GB";
String output = "kml";
RestResponse result = GoogleGeocodingService.geocode(strAddress, output);
InputStream is = new ByteArrayInputStream(result.getDataAsByteArray());
InputSource isrc = new InputSource(is);
DOMParser parser = new DOMParser();
parser.parse(isrc);
Document document = parser.getDocument();
document.getDocumentElement().normalize();
String coordinates = XPathAPI. eVVal(document, "/kml/Response/Placemark/Point/coordinates").toString();
System.out.println(coordinates);

Basically, the document is loaded and works fine (I have been able to print the tree view with aa recursive function) but XPath doesn't find the co-ordinates.

I have searched the forums and they suggest that the problem is to do with namespaces. I tried modifying my code s follows but I get an error. The code is:

String coordinates = XPathAPI. eVVal(document, "/kml/kml:Response/kml lacemark/kml oint/kml:coordinates").toString();

The error is:

Exception in thread "main" java.lang.Exception: Transaction failed
at com.compareanddive.test.BatchGeocoder.main(BatchGeocoder.java:108)
Caused by: org.apache.xpath.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: kml

If anyone can help me either fix this or the underlying problem with the Google web service mapping I would be very grateful.

Thanks,

mark
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You wouldn't have had that problem if you had posted your code in the CODE tags.

(And you are allowed to delete and edit posts that you made yourself.)

Anyway, XPath 1.0 can't find names that are in the default namespace, such as all the element names in that document. The way around that is to pretend they are in a named namespace, as you have done there. The other half of the way around is to provide the correct namespace context to the XPath engine. I'm not familiar with this "XPathAPI" class -- I can't see it in the standard Java API -- so I can't tell you how to do that.
Mark Varley
Greenhorn

Joined: May 20, 2003
Posts: 15
Ah, thanks for the tip on the CODE tags Paul, the error got from the site didn't make that clear - I will remember for next time as it was really frustrating. Does anyone know how I specify the namespace with XPath for Apache? I have tried with a PrefixResolver but can't seem to get it to work.

Thanks,

mark
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Just looking at what I think might be the documentation, PrefixResolver looks
like the right approach. Posting your code would be helpful, we are just
guessing here. (At least I am.)
Mark Varley
Greenhorn

Joined: May 20, 2003
Posts: 15
Hi there - code is as follos:



Again, it still can't seem to resolve the namespace (I also tried calling getNamespaceForPrefix("kml") on prfxrd and it returns null). My output is as follows:



I am really not sure where to go from here so any help would be most appreciated. Thanks, mark

[ UD: Added line breaks to preserve layout and disabled smilies. Please check the posts after sending them. ]
[ August 13, 2008: Message edited by: Ulf Dittmer ]
 
GeeCON Prague 2014
 
subject: Google Geocoder