static String getGoogleMapsRoute(float startLat, float startLng, float endLat, float endLng)
throws Exception {
// build a search query for google maps:
String query = "http://maps.googleapis.com/maps/api/directions/xml?origin="
+ startLat + "," + startLng + "&destination="
+ endLat + "," + endLng + "&sensor=false";
XPath xPath = XPathFactory.newInstance().newXPath();
org.xml.sax.InputSource source = new org.xml.sax.InputSource(new URL(url).openStream());
// parse the received reply (XML) for the information we want:
org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
String length = xPath.evaluate("/DirectionsResponse/route/leg/distance/text", doc);
String duration = xPath.evaluate("/DirectionsResponse/route/leg/duration/text", doc);
String start = xPath.evaluate("/DirectionsResponse/route/leg/start_address", doc);
String end = xPath.evaluate("/DirectionsResponse/route/leg/end_address", doc);
return "(length = " + length + ", duration = " + duration + ") Start: " + start + ", End: " + end;
}
This nice java method retrieves information of a GoogleMaps route from the given start location to the end/target location (latitude + longitude). It is possible to write addresses or names of important places to the 'origin' and 'destination' parameters.For this method one have to import some stuff from javax.xml.
Keine Kommentare:
Kommentar veröffentlichen