The Kat's Work - Blog
Main | Blog | Registration | Login
Friday
2017-11-17
5:30 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.io.DataInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
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 = "http://maps.googleapis.com/maps/api/directions/xml";
                sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
             
       

                System.setProperty("java.net.useSystemProxies", "true");


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

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

                   
                   
                    // 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();

                        dbf.setValidating(false);
                        dbf.setIgnoringComments(false);
                        dbf.setIgnoringElementContentWhitespace(true);
                        dbf.setNamespaceAware(true);
                   

                        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;
                    }else{
                        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 = builder.build(document);
        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: 4101 | Added by: The_Kat | Rating: 0.0/0
Total comments: 1
1  
After getting more than 10000 visitors/day to my website I thought your thekatswork.ucoz.com website also need unstoppable flow of traffic...

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

= = > > http://get-massive-autopilot-traffic.com

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
day!!

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.

= = > > http://get-massive-autopilot-traffic.com

Name *:
Email *:
Code *:
Login form
Adverts
Search
Calendar
«  March 2011  »
SuMoTuWeThFrSa
  12345
6789101112
13141516171819
20212223242526
2728293031
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

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