0

I'm trying to create an Android application that takes in an origin and destination and works along the lines of a GPS.

I was wondering if it was possible (through Google APIs) to display a map that shows the path between point A and B but also shows your current location on top of that path using GPS. I have seen tutorials and articles on how to display just the path between two points using Google directions and maps API but not combining that with your current location dot.

I have not really started this project yet because I am trying to figure out how to best approach this. Any help, tutorials, examples, suggestions will be appreciated!

Zack
  • 5,108
  • 4
  • 27
  • 39

2 Answers2

1

You must get the your current latitude and longitude first. You can check the link for the same in the below link

http://developer.android.com/guide/topics/location/strategies.html

You must fetch the latitude and longitudes between source and destination. Which should be done using asynctask.

new connectAsyncTask().execute()   

The asynctask class

 private class connectAsyncTask extends AsyncTask<Void, Void, Void>{
     private ProgressDialog progressDialog;
     @Override
     protected void onPreExecute() {
         // TODO Auto-generated method stub
         super.onPreExecute();
         progressDialog = new ProgressDialog(MainActivity.this);
         progressDialog.setMessage("Fetching route, Please wait...");
         progressDialog.setIndeterminate(true);
         progressDialog.show();
     }
     @Override
     protected Void doInBackground(Void... params) {
         // TODO Auto-generated method stub
         fetchData();
         return null;
     }
     @Override
     protected void onPostExecute(Void result) {
         super.onPostExecute(result);           
         if(doc!=null){
             NodeList _nodelist = doc.getElementsByTagName("status");
             Node node1 = _nodelist.item(0);
             String _status1  = node1.getChildNodes().item(0).getNodeValue();
             if(_status1.equalsIgnoreCase("OK")){
              Toast.makeText(MainActivity.this,"OK" , 1000).show();
                 NodeList _nodelist_path = doc.getElementsByTagName("overview_polyline");
                 Node node_path = _nodelist_path.item(0);
                 Element _status_path = (Element)node_path;
                 NodeList _nodelist_destination_path = _status_path.getElementsByTagName("points");
                 Node _nodelist_dest = _nodelist_destination_path.item(0);
                 String _path  = _nodelist_dest.getChildNodes().item(0).getNodeValue();
                 List<LatLng> points = decodePoly(_path);
                 for (int i = 0; i < points.size() - 1; i++) {
                   LatLng src = points.get(i);
                   LatLng dest = points.get(i + 1);
                    // Polyline to display the routes
                   Polyline line = mMap.addPolyline(new PolylineOptions()
                           .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude,dest.longitude))
                           .width(2).color(Color.BLUE).geodesic(true))   
               }
                 progressDialog.dismiss();
             }else{
                               // Unable to find route
                  }
         }else{
                         // Unable to find route
         }
     }
 }

DecodePoly function

   private List<LatLng> decodePoly(String encoded) {    
        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;
        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;
            LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
            poly.add(p);
        }
        return poly;
    }

Fetch data

Here flati and flongi is the source latitude and longitude

dlati and dlongi is the destination latitude and longitude

 Document doc = null;
 private void fetchData()
 {
     StringBuilder urlString = new StringBuilder();
     urlString.append("http://maps.google.com/maps/api/directions/xml?origin=");
     urlString.append( Double.toString(flati));
     urlString.append(",");
     urlString.append( Double.toString(flongi));
     urlString.append("&destination=");//to
     urlString.append( Double.toString(dlati));
     urlString.append(",");
     urlString.append( Double.toString(dlongi));
     urlString.append("&sensor=true&mode=walking");    
     Log.d("url","::"+urlString.toString());
     HttpURLConnection urlConnection= null;
     URL url = null;
     try
     {
         url = new URL(urlString.toString());
         urlConnection=(HttpURLConnection)url.openConnection();
         urlConnection.setRequestMethod("GET");
         urlConnection.setDoOutput(true);
         urlConnection.setDoInput(true);
         urlConnection.connect();
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = (Document) db.parse(urlConnection.getInputStream());//Util.XMLfromString(response);
     }catch (MalformedURLException e){
         e.printStackTrace();
     }catch (IOException e){
         e.printStackTrace();
     }catch (ParserConfigurationException e){
         e.printStackTrace();
     }
     catch (SAXException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }

enter image description here

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You can get an idea on how to implement Google Map API V2 in your application by reading this guide I wrote on this topic:

Google Map API V2

Then you could implement the driving navigation root using this answer I gave here:

Draw driving route between 2 GeoPoints on GoogleMap SupportMapFragment

and to find your current location you should implement a loicationListener, you can see an example here:

http://about-android.blogspot.co.il/2010/04/find-current-location-in-android-gps.html

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • din't you have this in your blog. – Raghunandan May 17 '13 at 18:07
  • @Raghunandan, have what in my blog? – Emil Adz May 17 '13 at 18:17
  • the tutorial regarding displaying routes between two points on the map. i also made a blog but it does not look good. yours looks nice. especially the black background. which blog are you using? – Raghunandan May 17 '13 at 18:19
  • It's Wordpress, and some free theme I picked up from their collection. If it's interests I could check which one is it. Regarding you question, No I hadn't had time to write on this topic yet. It's in my plans but had not done it yet. – Emil Adz May 17 '13 at 18:22
  • i wrote this blog http://raghunandankavi.blogspot.in/ looks horible, i used google's blogger. You can let me know of the theme – Raghunandan May 17 '13 at 18:24
  • the theme called: Dark Temptation, I don't know if it's exists in google's blogger. I think that the blog doesn't look so bad, your problem is that your are not using some kind of syntax editor, like I use: "SyntaxHighlighter Evolved", when you adding code to your post. – Emil Adz May 17 '13 at 18:29
  • Thanks will look into it and creative blog – Raghunandan May 17 '13 at 18:29