Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Google Maps - draw path between multiple points

What I want to do is this:

I receive a list of directions/paths (that the user will have to follow using my app). I am having trouble drawing the path on the map. The directions/paths contains the name of the streets, the coordinates of the streets and the segments of the streets.

I cant figure out how to draw the path/route on the map and make the route update - for example when the user moves (on the way) an icon to move indicating the progress of the user or the line drawn for the route gets shorter this really doesn't matter that much. So can you point me to tutorials which I can refer to?

I've seen a lot so far, but most of them get the directions from Google maps or the lines drawn are just straight lines from Start point to end point and doesn't fit the streets at all.

like image 285
user3182266 Avatar asked Jul 13 '26 14:07

user3182266


1 Answers

To achieve this, follow the below steps

  1. Get list of ArrayList markerPoints;

  2. Create your markers for it

  3. single path,

     LatLng origin = markerPoints.get(0);
     LatLng dest = markerPoints.get(1);
    
     // Getting URL to the Google Directions API
     String url = getDirectionsUrl(origin, dest);
    
     DownloadTask downloadTask = new DownloadTask();
    
     // Start downloading json data from Google Directions API
     downloadTask.execute(url);
    
  4. for multiple destination path, for example A-B-D-C etc

    private List<String> getDirectionsUrl(ArrayList<LatLng> markerPoints) {
    List<String> mUrls = new ArrayList<>();
    if (markerPoints.size() > 1) {
        String str_origin = markerPoints.get(0).latitude + "," + markerPoints.get(0).longitude;
        String str_dest = markerPoints.get(1).latitude + "," + markerPoints.get(1).longitude;
    
        String sensor = "sensor=false";
        String parameters = "origin=" + str_origin + "&destination=" + str_dest + "&" + sensor;
        String output = "json";
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
    
        mUrls.add(url);
        for (int i = 2; i < markerPoints.size(); i++)//loop starts from 2 because 0 and 1 are already printed
        {
            str_origin = str_dest;
            str_dest = markerPoints.get(i).latitude + "," + markerPoints.get(i).longitude;
            parameters = "origin=" + str_origin + "&destination=" + str_dest + "&" + sensor;
            url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
            mUrls.add(url);
        }
    }
    
    return mUrls;
    }
    
  5. Call the above method from

       List<String> urls = getDirectionsUrl(markerPoints);
       if (urls.size() > 1) {
              for (int i = 0; i < urls.size(); i++) {
                String url = urls.get(i);
                DownloadTask downloadTask = new DownloadTask();
               // Start downloading json data from Google Directions API
                        downloadTask.execute(url);
                    }
                }
        }
    

    the above code will call for to create multiple paths, like A-B, B-D, D-C etc

like image 162
Vivek Hande Avatar answered Jul 16 '26 07:07

Vivek Hande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!