3

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.

user2428118
  • 7,935
  • 4
  • 45
  • 72
user3182266
  • 1,270
  • 4
  • 23
  • 49

3 Answers3

2

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

Vivek Hande
  • 929
  • 9
  • 11
1

try following this tutorial. You should draw between user location and marker. On user side call function onLocationChange to get the actual position and redraw the line. http://wptrafficanalyzer.in/blog/driving-route-from-my-location-to-destination-in-google-maps-android-api-v2/

bogdand
  • 76
  • 1
  • 10
  • hmmm I think you did not understand the question. I dont want to draw the map using Google maps I want the route to be drawn by given points. – user3182266 Mar 07 '14 at 10:21
  • 1
    here is an example of how to draw route on map. The draw part it's at the final of the most usefull answer. http://stackoverflow.com/questions/14444228/android-how-to-draw-route-directions-google-maps-api-v2-from-current-location-t – bogdand Mar 07 '14 at 10:33
0

Follow this:Android Google Map V3 PolyLine cannot be drawn It'll help. You just need to parse the data received after hitting Google Directions API

Community
  • 1
  • 1
Adnan
  • 1,440
  • 2
  • 18
  • 38