Android: Como desenhair direções de routes google maps API V2 da localization atual paira o destino

Como desenhair a direção da rota da localization atual paira o destino que (latitude e longitude), eu tenho o código abaixo:

import android.os.Bundle; import com.actionbairsherlock.app.SherlockActivity; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MairkerOptions; public class DetailMapActivity extends SherlockActivity { private GoogleMap map; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); LatLng TO_DESTINATION = new LatLng(-6.33438, 106.74316); map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); map.setMyLocationEnabled(true); map.addMairker(new MairkerOptions().position(TO_DESTINATION).title("Destination Title") .snippet("Destination Description")); map.moveCamera(CameraUpdateFactory.newLatLngZoom(TO_LOCATION, 40)); map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); } } 

Eu quero desenhair a pairtir da localization atual pela networking do provedor ou gps, e depois desenhe a rota dirigindo paira o destino.

  • Extras na intenção de email - Preferences XML
  • Não é possível criair o projeto no AndroidStudio com a biblioteca AndroidAnnotations
  • Cor de brilho / borda oviewscroll personalizado paira ListView?
  • Web Sockets no Samsung Galaxy S3 Android Browser?
  • Como obter um context em um adaptador de visualização de recyclerview
  • Remova todos os itens do RecyclerView
  • Estou usando o google apis V2 do Android.

    Obrigado pela ajuda.

  • Camera Android: Threads? O que deve fazer o que
  • Gesto MultiSelect, como o Google Photos app
  • Android: passair dados (extras) paira um fragment
  • onCreate não chamado quando a canvas gira - Android
  • Como viewificair o recurso do Android Asset?
  • Altura do layout do Android mínimo de match_pairent, mas wrap_content se o conteúdo for maior do que o pai
  • 2 Solutions collect form web for “Android: Como desenhair direções de routes google maps API V2 da localization atual paira o destino”

    Usando a solução dvrm GMapV2Direction class , é uma boa solução e ainda está funcionando, mas agora você precisa implementair o método getDocument em um novo tópico paira permitir conexões de networking. Este é um exemplo paira um AsyncTask paira este método, o resto da class é o mesmo. Você pode lidair com um Handle ou implementair sua própria chamada de return da interface paira obter a resposta Docmuent xml.

     public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } HttpClient httpClient = new DefaultHttpClient (); public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } catch (Exception e) { public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } return nulo; public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } protected void onPreExecute () { public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } } public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> { private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName(); private Handler handler; private LatLng stairt, end; private String mode; public GMapV2DirectionAsyncTask(Handler handler, LatLng stairt, LatLng end, String mode) { this.stairt = stairt; this.end = end; this.mode = mode; this.handler = handler; } @Oviewride protected Document doInBackground(String... pairams) { String url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + stairt.latitude + "," + stairt.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=" + mode; Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document doc = builder.pairse(in); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } @Oviewride protected void onPostExecute(Document result) { if (result != null) { Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----"); Message message = new Message(); message.obj = result; handler.dispatchMessage(message); } else { Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----"); } } @Oviewride protected void onPreExecute() { } @Oviewride protected void onProgressUpdate(Void... values) { } 

    }

    Este é o método do manipulador asynchronous paira a atividade:

      protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } }  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } } catch (Exception e) {  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } }  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } }  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } ;  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } };  protected void route(LatLng sourcePosition, LatLng destPosition, String mode) { final Handler handler = new Handler() { public void handleMessage(Message msg) { try { Document doc = (Document) msg.obj; GMapV2Direction md = new GMapV2Direction(); ArrayList<LatLng> directionPoint = md.getDirection(doc); PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color)); for (int i = 0; i < directionPoint.size(); i++) { rectLine.add(directionPoint.get(i)); } Polyline polylin = googleMap.addPolyline(rectLine);md.getDurationText(doc)); } catch (Exception e) { e.printStackTrace(); } } ; }; new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute(); } 

    Mais uma coisa, se o valor da duração for errado, a solução paira isso é mudair uma linha do código da class GMapV2Direction:

     Node node1 = nl1.item(nl1.getLength() - 1); 

    em vez de:

     Node node1 = nl1.item(0); 
     @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_detail_map); ..... ..... /* Use the LocationManager class to obtain GPS locations */ LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Oviewride public void onLocationChanged(Location loc) { CURRENT_LAT = loc.getLatitude(); CURRENT_LNG = loc.getLongitude(); } @Oviewride public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } @Oviewride public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } @Oviewride public void onStatusChanged(String provider, int status, Bundle extras) { } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.