androidgoogle-mapsgoogle-maps-android-api-2google-directions-api

Is there any method to get a series of geopoints from source to destination in google directions api?


What I want is to get geo points from source to destination, distance between points can be 1 m. I think polylines are also made by connecting geo points between source and destination. What I don't know is can we get these geo points?


Solution

  • Indeed, you can get the list of LatLng that form a route in the Directions API. If you check the documentation for response format, you will see that there is legs[] array in each route, in its turn each leg has steps[] array and finally each step has polyline property. According to the documentation

    polyline contains a single points object that holds an encoded polyline representation of the step. This polyline is an approximate (smoothed) path of the step.

    The idea is to get a response, parse it as a JSON object and create loop for route by legs and steps. You should decode each polyline property of steps and add resulting LatLng to your list.

    There are several options to decode polyline:

    The Java client library for Google Maps Web services probably is the easiest way to implement it. The code snippet might be the following

    //Define list to get all latlng for the route
    List<LatLng> path = new ArrayList();
    
    //Execute Directions API request
    GeoApiContext context = new GeoApiContext.Builder()
            .apiKey("YOUR_API_KEY")
            .build();
    DirectionsApiRequest req = DirectionsApi.getDirections(context, "41.385064,2.173403", "40.416775,-3.70379");
    try {
        DirectionsResult res = req.await();
    
        //Loop through legs and steps to get encoded polylines of each step
        if (res.routes != null && res.routes.length > 0) {
            DirectionsRoute route = res.routes[0];
    
            if (route.legs !=null) {
                for(int i=0; i<route.legs.length; i++) {
                    DirectionsLeg leg = route.legs[i];
                    if (leg.steps != null) {
                        for (int j=0; j<leg.steps.length;j++){
                            DirectionsStep step = leg.steps[j];
                            if (step.steps != null && step.steps.length >0) {
                                for (int k=0; k<step.steps.length;k++){
                                    DirectionsStep step1 = step.steps[k];
                                    EncodedPolyline points1 = step1.polyline;
                                    if (points1 != null) {
                                        //Decode polyline and add points to list of route coordinates
                                        List<com.google.maps.model.LatLng> coords1 = points1.decodePath();
                                        for (com.google.maps.model.LatLng coord1 : coords1) {
                                            path.add(new LatLng(coord1.lat, coord1.lng));
                                        }
                                    }
                                }
                            } else {
                                EncodedPolyline points = step.polyline;
                                if (points != null) {
                                    //Decode polyline and add points to list of route coordinates
                                    List<com.google.maps.model.LatLng> coords = points.decodePath();
                                    for (com.google.maps.model.LatLng coord : coords) {
                                        path.add(new LatLng(coord.lat, coord.lng));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch(Exception ex) {
        Log.e(TAG, ex.getLocalizedMessage());
    }
    
    //Draw the polyline
    if (path.size() > 0) {
        PolylineOptions opts = new PolylineOptions().addAll(path).color(Color.BLUE).width(5);
        mMap.addPolyline(opts);
    }   
    

    You can download the complete sample project from https://github.com/xomena-so/so47492459

    Don't forget replace the API key with yours.

    I hope this helps!