GoogleMap map;
// ... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
Pero si quieres algo más dinámico que una simple línea recta, tienes que esforzarte y hacer algo así.
private class ReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
String data = "";
try {
data = FunctionUtilities.readUrl(url[0]);
} catch (Exception e) {
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
@Override
protected List<List<HashMap<String, String>>> doInBackground(
String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
FunctionUtilities parser = new FunctionUtilities();
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
ArrayList<LatLng> points = null;
PolylineOptions polyLineOptions = null;
for (int i = 0; i < routes.size(); i++) {
points = new ArrayList<LatLng>();
polyLineOptions = new PolylineOptions();
List<HashMap<String, String>> path = routes.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
polyLineOptions.addAll(points);
polyLineOptions.width(5);
polyLineOptions.color(Color.RED);
}
if (polyLineOptions != null) {
mapCU.addPolyline(polyLineOptions);
}
}
}
Y ese AssynTask lo mandas a llamar :
@Override
public void onMapReady(final GoogleMap googleMap) {
mapCU = googleMap;
mapUpdate(origPoint, destPoint);
}
private void mapUpdate(LatLng originPoint, LatLng destinationPoint) {
mapCU.clear();
mapCU.setMyLocationEnabled(false);
mapCU.getUiSettings().setMyLocationButtonEnabled(false);
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + originPoint.latitude + "," + originPoint.longitude
+ "&destination=" + destinationPoint.latitude + "," + destinationPoint.longitude + "&mode=walking";
new ReadTask().execute(url);
mapCU.moveCamera(CameraUpdateFactory.newLatLngZoom(originPoint, 16));
mapCU.addMarker(new MarkerOptions()
.position(originPoint)
.title("Origen")
.snippet(origin)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))).showInfoWindow();
mapCU.addMarker(new MarkerOptions()
.position(destinationPoint)
.title("Destino")
.snippet(destination)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mapCU.setMyLocationEnabled(true);
}
Puedes guiarte de esto:
Google Maps Api
Servicios Web Directions Api