Advertisement
Guest User

Get path between two paths

a guest
Dec 22nd, 2011
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
  2. {
  3. // connect to map web service
  4. StringBuilder urlString = new StringBuilder();
  5. urlString.append("http://maps.google.com/maps?f=d&hl=en");
  6. urlString.append("&saddr=");//from
  7. urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
  8. urlString.append(",");
  9. urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
  10. urlString.append("&daddr=");//to
  11. urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
  12. urlString.append(",");
  13. urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
  14. urlString.append("&ie=UTF8&0&om=0&output=kml");
  15. Log.d("xxx","URL="+urlString.toString());
  16. // get the kml (XML) doc. And parse it to get the coordinates(direction route).
  17. Document doc = null;
  18. HttpURLConnection urlConnection= null;
  19. URL url = null;
  20. try
  21. {
  22. url = new URL(urlString.toString());
  23. urlConnection=(HttpURLConnection)url.openConnection();
  24. urlConnection.setRequestMethod("GET");
  25. urlConnection.setDoOutput(true);
  26. urlConnection.setDoInput(true);
  27. urlConnection.connect();
  28.  
  29. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  30. DocumentBuilder db = dbf.newDocumentBuilder();
  31. doc = db.parse(urlConnection.getInputStream());
  32.  
  33. if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
  34. {
  35. //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
  36. String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
  37. Log.d("xxx","path="+ path);
  38. String [] pairs = path.split(" ");
  39. String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
  40. // src
  41. GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  42. mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1));
  43. GeoPoint gp1;
  44. GeoPoint gp2 = startGP;
  45. for(int i=1;i<pairs.length;i++) // the last one would be crash
  46. {
  47. lngLat = pairs[i].split(",");
  48. gp1 = gp2;
  49. // watch out! For GeoPoint, first:latitude, second:longitude
  50. gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
  51. mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
  52. Log.d("xxx","pair:" + pairs[i]);
  53. }
  54. mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color
  55. }
  56. }
  57. catch (MalformedURLException e)
  58. {
  59. e.printStackTrace();
  60. }
  61. catch (IOException e)
  62. {
  63. e.printStackTrace();
  64. }
  65. catch (ParserConfigurationException e)
  66. {
  67. e.printStackTrace();
  68. }
  69. catch (SAXException e)
  70. {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. Read more: http://csie-tw.blogspot.com/2009/06/android-driving-direction-route-path.html#ixzz1hKehHsDM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement