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.
|