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

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

RoutePath

  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 MapActivity.java 
     Marker marker1 = googleMap.addMarker(new MarkerOptions()
            .position(Point1)
            .title("Start")
            .snippet("Point 1")
            .visible(true)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    Marker marker2 = googleMap.addMarker(new MarkerOptions()
            .position(Point2)
            .anchor(0.5f, 0.5f)
            .title("End")
            .snippet("Point 2")
            .visible(true)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));

     

  3. Route path between point A and point B is determined using restful http Api, :-http://maps.googleapis.com/maps/api/directions/xml?origin=-36.889514,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 GMapV2GetRouteDirection.java
    String urlString = "http://maps.googleapis.com/maps/api/directions/xml?" + "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", "");
      connection.setRequestMethod("POST");
      connection.setDoInput(true);
      connection.connect();
    
      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 MapActivity.java
    protected void onPostExecute(String result) {
     //check if response is success from doInBackground
        if(response.equalsIgnoreCase("Success")){
          ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document);
         PolylineOptions rectLine = new PolylineOptions().width(10).color(Color.GREEN);
    
            for (int i = 0; i < directionPoint.size(); i++) {
                rectLine.add(directionPoint.get(i));
            }
            // Adding route on the map
            mGoogleMap.

     

    addPolyline

    (rectLine);

     

  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
    googleMap.animateCamera(cu);

     

  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 *