Bonjour,
J'ai essayée de dessiner un itinéraire entre deux points sur la carte google map .
Voila mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.example.projet_fin_etude;
 
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
 
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
 
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
 
public class MapDirection extends MapActivity{
 
	  MapView mapview;
	  MapRouteOverlay mapoverlay;
	  Context _context;
	  List<Overlay> maplistoverlay;
	  Drawable drawable,drawable2;
	  MapOverlay mapoverlay2,mapoverlay3;
	  GeoPoint srcpoint,destpoint;
	  Overlay overlayitem;
	  ArrayList <Place> myList =new ArrayList <Place>();
	  double lt;
		double lg;
	  public void onCreate(Bundle savedInstanceState){
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);  
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    setContentView(R.layout.activity_map_direction);
 
	    mapview=(MapView)this.findViewById(R.id.mapView);
	    Intent intent = getIntent();
		Bundle extras =intent.getExtras();
		if (extras != null){
			this.myList = (ArrayList<Place>) extras.getSerializable("myList");
			 lt=Double.parseDouble(intent.getStringExtra("latitude"));
			 lg=Double.parseDouble(intent.getStringExtra("longitude"));
 
	    callMap();
		}
	  }
	  private void callMap() {
 
 
	    srcpoint=new GeoPoint((int)(lt*1E6),(int)(lg*1E6));
	    maplistoverlay=mapview.getOverlays();
	    drawable=this.getResources().getDrawable(R.drawable.marq_dest);
	    mapoverlay2=new MapOverlay(drawable);
	    OverlayItem overlayitem = new OverlayItem(srcpoint, "", "");
	    mapoverlay2.addOverlay(overlayitem);
	    maplistoverlay.add(mapoverlay2);
 
	    destpoint=new GeoPoint((int)(myList.get(0).getLatitude()*1E6),(int)(myList.get(0).getLongitude()*1E6));
	    drawable2=this.getResources().getDrawable(R.drawable.marqueur1);
	    mapoverlay3=new MapOverlay(drawable2);
	    OverlayItem overlayitem3 = new OverlayItem(destpoint, "", "");
	    mapoverlay3.addOverlay(overlayitem3);
	    maplistoverlay.add(mapoverlay3);
 
 
	    double dest_lat = myList.get(0).getLatitude();
	    double dest_long = myList.get(0).getLongitude();
 
	    GeoPoint srcGeoPoint = new GeoPoint((int) (lt* 1E6),
	        (int) (lg * 1E6));
	    GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
	        (int) (dest_long * 1E6));
 
	  new DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapview).execute();
 
	    mapview.getController().animateTo(srcGeoPoint);
	    mapview.getController().setZoom(13);
	    //mapview.setStreetView(true);
	    mapview.setBuiltInZoomControls(true);
	    mapview.invalidate();
 
 
	  }
	  class DrawPath  extends AsyncTask<Void, Void, Document>
	       {
 
		  GeoPoint src;
		  GeoPoint dest;
		  int color;
		  MapView mMapView01;
		  public DrawPath(GeoPoint src, GeoPoint dest, int color,
			      MapView mMapView01) 
		  {
 
			  this.src=src;
			  this.dest=dest;
			  this.color=color;
			  this.mMapView01=mMapView01;
 
 
		  }
 
 
 
 
	    //System.out.println(urlString);
	    // get the kml (XML) doc. And parse it to get the coordinates(direction route).
 
 
	    	@Override
			protected Document doInBackground(Void... params) {
 
	    		//System.out.println(urlString);
	    	    // get the kml (XML) doc. And parse it to get the coordinates(direction route).
	    		Document doc = null;
	    	    HttpURLConnection urlConnection= null;
	    	    URL url = null;
	    	    // connect to map web service
	    	    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());
 
 
	    try
	    {
	      url = new URL(urlString.toString());
	      urlConnection=(HttpURLConnection)url.openConnection();
	      urlConnection.setRequestMethod("GET");
	      urlConnection.setDoOutput(true);
	      urlConnection.setDoInput(true);
	      urlConnection.connect();
 
	      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	      DocumentBuilder db = dbf.newDocumentBuilder();
	      doc = db.parse(urlConnection.getInputStream());
	    }
	    catch (MalformedURLException e)
	    {
	      e.printStackTrace();
	    }
	    catch (IOException e)
	    {
	      e.printStackTrace();
	    }
	    catch (ParserConfigurationException e)
	    {
	      e.printStackTrace();
	    }
	    catch (SAXException e)
	    {
	      e.printStackTrace();
	    }
	    return doc;
	    	}
	    	  protected void onPostExecute(Document result){
 
	      if(result.getElementsByTagName("GeometryCollection").getLength()>0)
	      {
	        //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
	        String path = result.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(overlayitem);
	        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 MapRouteOverlay(gp1,gp2,2,color));
	          Log.d("xxx","pair:" + pairs[i]);
	        }
	        //mMapView01.getOverlays().add(new MapRouteOverlay(dest,dest, 3)); // use the default color
	      }
	    }
 
 
 
	  }
	  @Override
	    protected boolean isRouteDisplayed() {
	      // TODO Auto-generated method stub
	      return false;
	    }
	}
La carte s'affiche ainsi que les deux marqueurs mais l'activité s’arrête et l'itinéraire ne s'affiche pas et j'obtient ces erreurs dans le logcat:

04-28 21:51:53.544: W/System.err(922): org.xml.sax.SAXParseException: expected: /meta read: noscript (position:END_TAG </noscript>@1:493 in java.io.InputStreamReader@40f77098)
04-28 21:51:53.554: W/System.err(922): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
04-28 21:51:53.554: W/System.err(922): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)
04-28 21:51:53.554: W/System.err(922): at com.example.projet_fin_etude.MapDirection$DrawPath.doInBackground(MapDirection.java:165)
04-28 21:51:53.575: W/System.err(922): at com.example.projet_fin_etude.MapDirection$DrawPath.doInBackground(MapDirection.java:1)
04-28 21:51:53.575: W/System.err(922): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-28 21:51:53.575: W/System.err(922): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-28 21:51:53.604: W/System.err(922): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-28 21:51:53.604: W/System.err(922): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-28 21:51:53.604: W/System.err(922): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-28 21:51:53.624: W/System.err(922): at java.lang.Thread.run(Thread.java:856)
04-28 21:51:53.624: D/AndroidRuntime(922): Shutting down VM
Avez vous des idées s'il vous plait? et merci bien d'avance .