Android Apps – Route path between two points using Google Map Api

Android Apps – Route path between two points using Google Map Api


  1. Just a few points on using google map v2 api
  2. Markers are used in google map api to mark the locations. In this example location A is marked with red icon with latitude/longtitude ( -36.889514,174.825917) and location B with blue icon with latitude/longitude (-36.889514,174.825917)  .. you’re find this in code file 
     Marker marker1 = googleMap.addMarker(new MarkerOptions()
            .snippet("Point 1")
    Marker marker2 = googleMap.addMarker(new MarkerOptions()
            .anchor(0.5f, 0.5f)
            .snippet("Point 2")


  3. Route path between point A and point B is determined using restful http Api, :-,174.825917&destination=-36.885735,174.762360&sensor=false&units=metric&mode=driving
    1. you should get a xml output of the route path. Click here to see the output.
  4. The execution of restful http Api use HttpURLConnection class object to read the XML response from previous step 4 and place this in a DOM object  .. in code you’re find this in
    String urlString = "" + "origin=" + start.latitude + "," + start.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=driving";
    try {
      URL urlxa = new URL(urlString);
      HttpURLConnection connection = (HttpURLConnection)urlxa.openConnection();
      connection.setRequestProperty("User-Agent", "");
      InputStream in = connection.getInputStream();
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;


  5. Finally once the application received the dom document of the route path between point A & point B, addpolyline method is used to draw the route path on android application .. in the code you see this in file
    protected void onPostExecute(String result) {
     //check if response is success from doInBackground
          ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document);
         PolylineOptions rectLine = new PolylineOptions().width(10).color(Color.GREEN);
            for (int i = 0; i < directionPoint.size(); i++) {
            // Adding route on the map





  6. The android application use the cameraupdate function to show this on Android mobile .. in the code you find this in MapActivity.javaCameraUpdate
     cu = CameraUpdateFactory.newLatLngBounds(bounds, 665, 665, 5);
    //show the locations on google map


  7. You can find the full listing of the android google map code by clicking here.
    1. Other important file would be AndriodManifest.xml 

One Responseso far.

  1. Useful! Fascinating! Quality! Belongings! Legendary! Wonderful! Remarkable! Merely all the nice words apply to this material! If you maintain up the excellent work, thank you from the base of my heart and also soul as well as I will come see once again!

Leave a Reply

Your email address will not be published. Required fields are marked *