0

Hi Guys i am working on Google maps Implementation.

I am trying to draw path via google maps.I have button to start and stop the GPS, basically to get Lat and Long values.

Eg : i may get 10 lat and long values and i store them in ArrayList.

Now i am trying to draw a path with the above list of lat and long, If i use the Google maps API then i need to send only start and end points of my ArrayList and then it draws/renders on the map,but its not the way i wanted.

I wanted to draw the path along the list of lat and long values (10 values).The reason is google just gives us the path from start to end but it might not be the way/route in which the user has travelled.

How can we do this ?

Below is what i am doing :

private String getMapsApiDirectionsUrl(LatLng source, LatLng destination, String mode) {
        // Origin of route
        String str_origin = "origin=" + source.latitude + "," + source.longitude;
        // Destination of route
        String str_dest = "destination=" + destination.latitude + "," + destination.longitude;
        // Sensor enabled
        String sensor = "sensor=false";
        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode + "&alternatives=true";
        // Output format
        String output = "json";
        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

        Log.e(TAG, " " + url);
        return url;
    }

private class ReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... url) {
            String data = "";
            try {
                HttpConnection http = new HttpConnection();
                data = http.readUrl(url[0]);
            } catch (Exception e) {
                Log.d("Background Task", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            new ParserTask().execute(result);
        }
    }

    private class ParserTask extends
            AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

        @Override
        protected List<List<HashMap<String, String>>> doInBackground(
                String... jsonData) {

            JSONObject jObject;
            List<List<HashMap<String, String>>> routes = null;

            try {
                jObject = new JSONObject(jsonData[0]);
                PathJSONParser parser = new PathJSONParser();
                routes = parser.parse(jObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return routes;
        }

        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
            task.callBack(routes);
        }
    }

EDIT

i am doing exactly the way its shown here, but what happens is based on the start and end values Google maps APi V2 gives me the route but that might not be the route that user has travelled, as he has might have visited some places in between the start and end.

Community
  • 1
  • 1
user2056563
  • 600
  • 2
  • 12
  • 37
  • 1
    Possible duplicate of http://stackoverflow.com/questions/14710744/how-to-draw-road-directions-between-two-geocodes-in-android-google-map-v2/16315944#16315944 – Jay Rathod Mar 14 '16 at 12:05
  • @jaydroider yes i am doing exactly the way its shown there, but what happens is based on the start and end values Google maps APi V2 gives me the route but that might not be the route that user has travelled, as he has might have visited some places in between the start and end. – user2056563 Mar 14 '16 at 12:10
  • I think you need to create service that will get user's location at some interval and by passing that location to Google API (https://maps.googleapis.com/maps/api/directions/json?str_origin&str_dest&sensor=false) at some interval you will draw path as user have traveled. – Dhruv Mar 14 '16 at 12:15

3 Answers3

2

ok what i get from your question is you have 10 latlong and you want to draw them in single route. here is the solution-

  • create arraylist of latlong
  • assign the latlong values into the arraylist
  • add this arraylist to an polylineoptions
  • draw it on map

Programmatically

ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = new PolylineOptions()
points.add(position); //positions are the latlng;
lineOptions.addAll(points);
            lineOptions.width(8);
            lineOptions.color(Color.RED);
googleMap.addPolyline(lineOptions); //googleMap is the googlemap object which you are displaying on activity.

good luck.

please let me know if this is what you want.

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • Thanks, but it adds straight line and also the lines go off the road.So if i use Map API then it adds curved lines but it draws path based on Start and end points and not all the 10 points. – user2056563 Mar 14 '16 at 12:41
  • please do this in code first . this will never draw a straight line. the line will start from the first latlng you provided and end at the last latlng . and also this will go through all the points you given . the straight lines will be drawn from one latlng to the next latlng not from the source to destination. and if you need more precise route then you need more points along the route. – Sagar Nayak Mar 14 '16 at 12:44
  • I tried and the path is off the route,i meant off the road/driving directions. – user2056563 Mar 14 '16 at 12:56
  • yup this is not compulsory that the path will on the route. because you the path is being drawn according to your provided latlng and it dont know where the path is. – Sagar Nayak Mar 14 '16 at 13:15
  • if you want to get the path on the road and that path will go through your specific latlng then you have to resend queries to direction api and those points you mentioned must be on the road or near to the road. – Sagar Nayak Mar 14 '16 at 13:27
0

here is what i m doing to draw a path in googlemap using polyline

  LatLng prev = new LatLng(pLati, plongi);
    LatLng my = new LatLng(latid, longid);

    Polyline line = googlemap.addPolyline(new PolylineOptions()
            .add(prev, my).width(5).color(Color.RED));
Sagar
  • 585
  • 1
  • 9
  • 28
0

You can do by following solution.

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < list.size(); i++) {
    LatLng point = list.get(i);
    options.add(point);
}

Polyline line = myMap.addPolyline(options);

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151