Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing driving routes using waypoints on android ( Google maps, Google direction api, json parsing, decode google polyline)

drawPath() is used to draw polyloine on map.

public void drawPath(String result){
        try {
            final JSONObject jsonObject = new JSONObject(result);

            JSONArray routeArray = jsonObject.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);


            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");

            String statusString = jsonObject.getString("status");

            Log.d("test: ", encodedString);
            List<LatLng> list = decodePoly(encodedString);

            LatLng last = null;
            for (int i = 0; i < list.size()-1; i++) {
                LatLng src = list.get(i);
                LatLng dest = list.get(i+1);
                last = dest;
                Log.d("Last latLng:", last.latitude + ", " + last.longitude );
                Polyline line = mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                .width(4)
                .color(Color.GREEN));
            }

            Log.d("Last latLng:", last.latitude + ", " + last.longitude );
        }catch (JSONException e){
            e.printStackTrace();
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
        }
    }

    private List<LatLng> decodePoly(String encoded){


        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0;
        int length = encoded.length();

        int latitude = 0;
        int longitude = 0;

        while(index < length){
            int b;
            int shift = 0;
            int result = 0;

            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);

            int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            latitude += destLat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b > 0x20);

            int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            longitude += destLong;

            poly.add(new LatLng((latitude / 1E5),(longitude / 1E5) ));
        }
        return poly;
    }

Now my problem is route is not displayed correctly in the map as it has waypoints in the link. I am using it on android 4.2

My link is http://maps.googleapis.com/maps/api/directions/json?origin=18.XXX,73.XXXdestination=18.XXX,73.XXX&sensor=false&units=metric&mode=driving&waypoints=via:18.XXX,73.XXX

like image 320
code_guru Avatar asked Mar 14 '13 10:03

code_guru


People also ask

Can I draw a route on Google Maps Android?

To create a route in Google Maps, open "Your places" and then choose "Create Map" in the "Maps" tab. To draw a route, click "Add directions," choose transportation mode, and enter start and end points. You can draw lines and shapes on maps by clicking "Draw a line" and selecting "Add line or shape."

Is there an app where I can draw my route?

Footpath is the ultimate companion for planning and navigating custom routes. Join millions of adventurers and plan out your perfect route. Quickly measure distances by tracing a map with your finger or Apple Pencil. Footpath will snap to roads and trails on the map.


2 Answers

I have tested your decodePoly part. seems no problem there. you can use steps and legs to be more precise.

like image 149
Tejzeratul Avatar answered Nov 15 '22 04:11

Tejzeratul


@Zeratul thanks for decoding code over overview_polyline.points. That works great. By the way, after getting list of LatLng, you don't need to loop over list. Just use addAll method of PolylineOptions.

List<LatLng> list = decodePoly(encodedString);
PolylineOptions po = new PolylineOptions();
po.addAll(list);
po.width(2).color(Color.BLUE);
Polyline line = getMap().addPolyline(po);
like image 35
Devrim Avatar answered Nov 15 '22 03:11

Devrim