Not so long ago, I needed to write an application that would show the route between two given points on the map. As a tool were selected maps from Google. After a brief search, articles were found about the
integration of maps into the application, about
pin filtering , about
routes . However, there was no article describing how to make such routes in the Android application, and I decided to correct this omission (for new developers for Android, it will come in handy). Welcome under habrakat.
Suppose we have already read an article about integration, got an API-Key, and wrote an application with a map with two points marked on it. Therefore, it only remains for us to lay a route between two given points.
Getting a route
')
First of all, you need to send a request and get a file with the route data. To do this, we will need to open the connection by the specified link (the latitude and longitude in the link are separated by commas) and receive the data in the form of XML. We will pull out the route points from the file, we will be the DOM parser, for which we create the Document object.
String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr=" + startGeoPoint + "&daddr=" + stopGeoPoint + "&ie=UTF8&0&om=0&output=kml";
Document document = null;
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuildFactory.newDocumentBuilder();
document = documentBuilder.parse(urlConnection.getInputStream());
} catch (Exception e) { }
Following the KML
documentation , the route consists of smaller segments, so we have to create an array of points through which our route will pass. Details on parsing the XML file can be found in
this article. It is worth noting that the points are in the body of the "coordinates" tag, which, in turn, is inside the "LineString".
String points = null;
NodeList nodeList = document.getElementsByTagName("LineString");
points = nodeList.item(0).getFirstChild().getFirstChild().getNodeValue();
String[] finalArray = points.split(" ");
On this receipt of the coordinates for which the route will be built is completed. Moving on to drawing the line itself.
Overlay route lines
This class inherits Overlay and is (roughly speaking) a graphic level that will be displayed on the map. In this class, we will create a constructor through which we will set the start and end points of the line, and we will add a draw method that will draw this line.
private GeoPoint start;
private GeoPoint stop;
public MapOverlay(GeoPoint start, GeoPoint stop) {
this.start = start;
this.stop = stop;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
// TODO Auto-generated method stub
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(start, point);
paint.setColor(Color.BLUE);
Point point2 = new Point();
projection.toPixels(stop, point2);
paint.setStrokeWidth(2);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x, (float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
And now we need to write a method that will add points to this level, creating our route.
Creating a route
If you have reached this point, then, most likely, you know that GeoPoint first indicates the latitude, then the longitude.
String coords = finalArray[0].split(",");
GeoPoint start = new GeoPoint((int) (Double.parseDouble(coords[1]) * 1E6), (int) (Double.parseDouble(coords[0]) * 1E6));
// , , ,
mapView.getOverlays().add(new MapOverlay(start, start));
GeoPoint geoPoint1;
GeoPoint geoPoint2 = start;
for (int i = 1; i < finalArray.length; i++) {
coords = finalArray[i].split(",");
geoPoint1 = geoPoint2;
geoPoint2 = new GeoPoint((int) (Double.parseDouble(coords[1]) * 1E6), (int) (Double.parseDouble(coords[0]) * 1E6));
mapView.getOverlays().add(new MapOverlay(geoPoint1, geoPoint2));
}
mapView.getOverlays().add(new MapOverlay(geoPoint2, geoPoint2));
This completes the creation of the route. Thanks for attention. As an example, I will give a screenshot of what happened to me.

PS This is my first article, so please do not judge strictly, since I don’t have much experience writing articles, and the code was explained only with words and on fingers.