2010-06-01 20 views
0

En ce moment j'utilise ce code pour demander des directions de google maps d'une adresse à un autre, alors je le dessine simplement sur un mapview de son GeometryCollection. Mais cela ne suffit pas, je dois aussi extraire la durée totale attendue du kml. quelqu'un peut-il donner un petit exemple de code pour m'aider? merciandroid obtenir la durée de maps.google.com directions

StringBuilder urlString = new StringBuilder(); 
urlString.append("http://maps.google.com/maps?f=d&hl=en"); 
urlString.append("&saddr=");//from 
urlString.append(Double.toString((double)src.getLatitudeE6()/1.0E6)); 
urlString.append(","); 
urlString.append(Double.toString((double)src.getLongitudeE6()/1.0E6)); 
urlString.append("&daddr=");//to 
urlString.append(Double.toString((double)dest.getLatitudeE6()/1.0E6)); 
urlString.append(","); 
urlString.append(Double.toString((double)dest.getLongitudeE6()/1.0E6)); 
urlString.append("&ie=UTF8&0&om=0&output=kml"); 
//Log.d("xxx","URL="+urlString.toString()); 
// get the kml (XML) doc. And parse it to get the coordinates(direction route). 
Document doc = null; 
HttpURLConnection urlConnection= null; 
URL url = null; 
try 
{ 
    url = new URL(urlString.toString()); 
    urlConnection=(HttpURLConnection)url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setDoOutput(true); 
    urlConnection.setDoInput(true); 
    urlConnection.connect(); 
    dbf = DocumentBuilderFactory.newInstance(); 
    db = dbf.newDocumentBuilder(); 
    doc = db.parse(urlConnection.getInputStream()); 
    if(doc.getElementsByTagName("GeometryCollection").getLength()>0) 
    { 
     //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName(); 
     String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ; 
     //Log.d("xxx","path="+ path); 
     String[] pairs = path.split(" "); 
     String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height 
     // src 
     GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6)); 
     mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1)); 
     GeoPoint gp1; 
     GeoPoint gp2 = startGP; 
     for(int i=1;i<pairs.length;i++) // the last one would be crash 
     { 
      lngLat = pairs[i].split(","); 
      gp1 = gp2; 
      // watch out! For GeoPoint, first:latitude, second:longitude 
      gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6)); 
      mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color)); 
      //Log.d("xxx","pair:" + pairs[i]); 
     } 
     mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color 
     } 
    }catch (MalformedURLException e){ 
     e.printStackTrace(); 
    }catch (IOException e){ 
     e.printStackTrace(); 
    }catch (SAXException e){ 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } 
} 

Répondre

6

Utilisez la nouvelle API Directions. La durée est renvoyée dans la réponse et pas plus l'analyse de KML, qui je pense casse les termes de la licence Google Maps de toute façon.

Mise à jour. Ok, utilisez la sortie JSON, avec l'analyse est intégrée dans Android.

Ex url

réponse est

{ 
    "status": "OK", 
    "routes": [ { 
    "summary": "I-40 W", 
    "legs": [ { 
     "steps": [ { 
     "travel_mode": "DRIVING", 
     "start_location": { 
      "lat": 41.8507300, 
      "lng": -87.6512600 
     }, 
     "end_location": { 
      "lat": 41.8525800, 
      "lng": -87.6514100 
     }, 
     "polyline": { 
      "points": "[email protected]", 
      "levels": "B?B" 
     }, 
     "duration": { 
      "value": 19, 
      "text": "1 min" 
     }, 
     "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e", 
     "distance": { 
      "value": 207, 
      "text": "0.1 mi" 
     } 
     }, 
     ... 
     ... additional steps of this leg 
    ... 
    ... additional legs of this route 
     "duration": { 
     "value": 74384, 
     "text": "20 hours 40 mins" 
     }, 
     "distance": { 
     "value": 2137146, 
     "text": "1,328 mi" 
     }, 
     "start_location": { 
     "lat": 35.4675602, 
     "lng": -97.5164276 
     }, 
     "end_location": { 
     "lat": 34.0522342, 
     "lng": -118.2436849 
     }, 
     "start_address": "Oklahoma City, OK, USA", 
     "end_address": "Los Angeles, CA, USA" 
    } ], 
    "copyrights": "Map data ©2010 Google, Sanborn", 
    "overview_polyline": { 
     "points": "[email protected][email protected]`vnApw{A`[email protected]~w\\|[email protected]{[email protected]@b}@[email protected][email protected]@jc|Bx}C`[email protected]|@[email protected]}Axf][email protected][email protected]{A~d{A|[email protected]`cFp~xBc`[email protected]@[email protected][email protected]@[email protected]|{CbtY~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb~uF{cNd}xBjp][email protected]|[email protected]_Kv~eGyqTj_|@`uV`k|[email protected]}[email protected][email protected]`CnvHx`[email protected]@j|[email protected]|[email protected]`[email protected][email protected]}[email protected]`@|}[email protected]@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC", 
     "levels": "BBBAAAAABAABAAAAAABBAAABBAAAABBAAABABAAABABBAABAABAAAABABABABBABAABB" 
    }, 
    "warnings": [ ], 
    "waypoint_order": [ 0, 1 ] 
    } ] 
} 

Disons que vous utilisé les bibliothèques standard du client http et ont maintenant la réponse dans une chaîne.

JSONObject jsonObject = new JSONObject(response); // parse response into json object 
JSONObject routeObject = jsonObject.getJSONObject("route"); // pull out the "route" object 
JSONObject durationObject = jsonObject.getJSONObject("duration"); // pull out the "duration" object 
String duration = durationObject.getString("text"); //this should be the duration text value (20 hours 40 mins) 

Vous devrez envelopper tout cela dans un bloc try-catch pour attraper le JSONExceptions.

+0

ok je lis cette page http://code.google.com/apis/maps/documentation/directions/#XML qui contient l'explication ce que je ne sais comment pas que ce soit d'interpréter ce fichier: par cela je veux dire que pour le kml il y a une balise GeometryCollection contenant l'itinéraire savoir comment dessiner dans la carte. et ce qui est cette ** Chaîne unreadble humaine ** ... grâce – urobo

+0

Mis à jour pour montrer par exemple. –