Get distance and time in list of addresses

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Get distance and time in list of addresses

Postby miguel » Fri Jan 08, 2010 11:40 am

Hi, in my applicattion I'm trying to get the distance and time from my destination to addresses that are in contacts and put the result in a list view. (like this http://androidpt.com/images/fbfiles/ima ... 100108.png )

I've tried the aproach of reading this URL http://maps.google.com/maps?f=d&hl=en&s ... output=kml in kml format and then extract the info that I want (Example: Distance: 44.7km (about 54mins)).

The problem is that it takes about one second to process one address. So if I've 20 contacts with addresses it will take 20seconds.

Is there another way to accomplish what i'm pretending to doing?

Here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. StringBuilder urlString = new StringBuilder();
  2.  
  3. urlString.append("http://maps.google.com/maps?f=d&hl=en");
  4.  
  5. urlString.append("&saddr=");//my location
  6.  
  7. urlString.append(mylocation);
  8.  
  9. urlString.append("&daddr=");//contact address
  10.  
  11. urlString.append(contactAddress);
  12.  
  13. urlString.append("&ie=UTF8&0&om=0&output=kml");
  14.  
  15.  
  16.  
  17. Document doc = null;
  18.  
  19. HttpURLConnection urlConnection= null;
  20.  
  21. URL url = new URL(urlString.toString());
  22.  
  23. urlConnection=(HttpURLConnection)url.openConnection();
  24.  
  25. try {
  26.  
  27.         urlConnection.setRequestMethod("GET");
  28.  
  29. } catch (ProtocolException e) {
  30.  
  31.                         // TODO Auto-generated catch block
  32.  
  33.                         e.printStackTrace();
  34.  
  35. }
  36.  
  37. urlConnection.setDoOutput(true);
  38.  
  39. urlConnection.setDoInput(true);
  40.  
  41. urlConnection.connect();
  42.  
  43.  
  44.  
  45. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  46.  
  47. DocumentBuilder db = dbf.newDocumentBuilder();
  48.  
  49. doc = db.parse(urlConnection.getInputStream());
  50.  
  51.  
  52.  
  53. Element topElement = doc.getDocumentElement();
  54.  
  55. NodeList description = topElement.getElementsByTagName("description");
  56.  
  57. Element result = (Element) description.item(placemark.getLength()-1);
  58.  
  59. String distanceAndTime = route.getFirstChild().getNodeValue();                
  60.  
  61.            
  62.  
  63. String[] splitResult = distanceAndTime.split("<br/>");
  64.  
  65. distanceAndTime = splitResult[0].replace(" "," ");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thanks in adavance
miguel
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Sep 01, 2008 3:59 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests