The Kat's Work - Blog
Main | Blog | Registration | Login
4:31 PM
Welcome Guest | RSS
Main » 2011 » March » 11 » Find Driving Distance Java Class
9:53 AM
Find Driving Distance Java Class
I have wrote a rather nifty Java Class that will allow you to specify two locations and find the driving distance between them.

It uses the google api on supplied values. For the best result supply longitude latitude figures (e.g "53.453194,-2.726241") however postcodes will be accepted.

import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class GoogleDistance {
    public static Double retrieveMeters(String startLatitudeLongitude, String endLatitudeLongitude) throws Exception{
                String version = "2.3";
                String agent = "Mozilla/4.0";
                String respText = "";
                HashMap nvp = null; //lhuynh not used?

                //deformatNVP( nvpStr );
                String apiEndPoint = "";
                sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

                System.setProperty("", "true");

                String data = "origin=" + startLatitudeLongitude + "&destination=" + endLatitudeLongitude + "&sensor=false";
                   URL postURL = new URL( apiEndPoint + "?" + data );
                    HttpURLConnection conn = (HttpURLConnection)postURL.openConnection();

                    conn.setDoInput (true);
                    conn.setDoOutput (true);
                    conn.setRequestProperty( "User-Agent", agent );

                    // get the output stream to POST to.
                   Double meters;
                   // Read input from the input stream.
                    DataInputStream in = new DataInputStream (conn.getInputStream());
                    int rc = conn.getResponseCode();
                    if ( rc != -1)
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();


                        DocumentBuilder docBuilder = null;
                        docBuilder = dbf.newDocumentBuilder();
                        docBuilder.setEntityResolver(new NullResolver());                   

                        org.w3c.dom.Document xmlDoc = docBuilder.parse(in);
                        org.jdom.Document xmlJDoc = GoogleDistance.convert(xmlDoc);
                        org.jdom.Element directionResponse = xmlJDoc.getRootElement();
                        org.jdom.Element route = directionResponse.getChild("route");
                        org.jdom.Element leg = route.getChild("leg");
                        org.jdom.Element distance = leg.getChild("distance");
                        org.jdom.Element value = distance.getChild("value");
                        meters = Double.valueOf(((org.jdom.Text)value.getContent().get(0)).getText());

                    return meters;
                        Exception e = new Exception();
                      throw e; 

     public static org.jdom.Document convert( org.w3c.dom.Document document)
        throws JDOMException, IOException {
        // Create new DOMBuilder, using default parser
        DOMBuilder builder = new DOMBuilder();
        org.jdom.Document jdomDoc =;
        return jdomDoc;

    class NullResolver implements EntityResolver {
          public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
              IOException {
            return new InputSource(new StringReader(""));

Its also a good example of the use of the HttpURLConnection class to call APIs from a servelet.

As you can see I convert the org.w3c.dom.Document xml document to org.jdom.Document, which I find much simpler to work with.

Views: 4233 | Added by: The_Kat | Rating: 0.0/0
Total comments: 1
0 Spam
1 Orefvurse  
After getting more than 10000 visitors/day to my website I thought your website also need unstoppable flow of traffic...

Use this BRAND NEW software and get all the traffic for your website you will ever need ...

= = > >

In testing phase it generated 867,981 visitors and $540,340.

Then another $86,299.13 in 90 days to be exact. That's $958.88 a

And all it took was 10 minutes to set up and run.

But how does it work??

You just configure the system, click the mouse button a few
times, activate the software, copy and paste a few links and
you're done!!

Click the link BELOW as you're about to witness a software that
could be a MAJOR turning point to your success.

= = > >

Name *:
Email *:
Code *:
Login form
«  March 2011  »
Entries archive
Site friends
  • Create your own site
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Copyright MyCorp © 2019
    Website builderuCoz