2

i followed the tut in this here https://inducesmile.com/android/android-find-distance-and-duration-between-two-points-on-android-map/

everything seems to be working , only 2 error that i got and everyone who followed the tut

first in MapsActivity

private List<LatLng> getDirectionPolylines(List<RouteObject> routes){
        List<LatLng> directionList = new ArrayList<LatLng>();
        for(RouteObject route : routes){
            List<LegsObject> legs = route.getLegs();
            for(LegsObject leg : legs){
                String routeDistance = leg.getDistance().getText(); HERE
                String routeDuration = leg.getDuration().getText(); HERE
                setRouteDistanceAndDuration(routeDistance, routeDuration); // here we will send the route Duration and distent
                List<StepsObject> steps = leg.getSteps();
                for(StepsObject step : steps){
                    PolylineObject polyline = step.getPolyline();
                    String points = polyline.getPoints();
                    List<LatLng> singlePolyline = decodePoly(points);
                    for (LatLng direction : singlePolyline){
                        directionList.add(direction);
                    }
                }
            }
        }
        return directionList;
    } 

error

Cannot resolve method 'getText()

second error is in LegsObject class

import java.util.List;


public class LegsObject {
    private List<StepsObject> steps;

    private DistanceObject distance;

    private DurationObject duration;

    public LegsObject(DurationObject duration, DistanceObject distance, List<StepsObject> steps) {
        this.duration = duration;
        this.distance = distance;
        this.steps = steps;
    }

    public List<StepsObject> getSteps() {
        return steps;
    }

    public DistanceObject getDistance() {
        return distance;
    }

    public DurationObject getDuration() {
        return duration;
    }
}

error

Cannot resolve symbol 'DistanceObject'
Cannot resolve symbol 'DurationObject'

i believe if the second error in LegsObject.class fixed the first error will be fixed as well

Lim Cho
  • 103
  • 2
  • 11

2 Answers2

4

You can use a third party library for this. It's easy and efficient:

Gradle Dependancy:

compile 'com.akexorcist:googledirectionlibrary:1.0.4' // Custom Google Direction API \\

Code:

Below method will take the latLng of your destination, also inside method you should have latlng object containing your origin. Server key is your api key and you should also enable Google Directions API to make this work.

/**
     * Draw polyline on map, get distance and duration of the route
     *
     * @param latLngDestination LatLng of the destination
     */
    private void getDestinationInfo(LatLng latLngDestination) {
        progressDialog();
        String serverKey = getResources().getString(R.string.google_direction_api_key); // Api Key For Google Direction API \\
        final LatLng origin = new LatLng(latitude, longitude);
        final LatLng destination = latLngDestination;
        //-------------Using AK Exorcist Google Direction Library---------------\\
        GoogleDirection.withServerKey(serverKey)
                .from(origin)
                .to(destination)
                .transportMode(TransportMode.DRIVING)
                .execute(new DirectionCallback() {
                    @Override
                    public void onDirectionSuccess(Direction direction, String rawBody) {
                        dismissDialog();
                        String status = direction.getStatus();
                        if (status.equals(RequestResult.OK)) {
                            Route route = direction.getRouteList().get(0);
                            Leg leg = route.getLegList().get(0);
                            Info distanceInfo = leg.getDistance();
                            Info durationInfo = leg.getDuration();
                            String distance = distanceInfo.getText();
                            String duration = durationInfo.getText();

                            //------------Displaying Distance and Time-----------------\\
                            showingDistanceTime(distance, duration); // Showing distance and time to the user in the UI \\
//                            String message = "Total Distance is " + distance + " and Estimated Time is " + duration;
//                            StaticMethods.customSnackBar(consumerHomeActivity.parentLayout, message,
//                                    getResources().getColor(R.color.colorPrimary),
//                                    getResources().getColor(R.color.colorWhite), 3000);

                            //--------------Drawing Path-----------------\\
                            ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
                            PolylineOptions polylineOptions = DirectionConverter.createPolyline(getActivity(),
                                    directionPositionList, 5, getResources().getColor(R.color.colorPrimary));
                            googleMap.addPolyline(polylineOptions);
                            //--------------------------------------------\\

                            //-----------Zooming the map according to marker bounds-------------\\
                            LatLngBounds.Builder builder = new LatLngBounds.Builder();
                            builder.include(origin);
                            builder.include(destination);
                            LatLngBounds bounds = builder.build();

                            int width = getResources().getDisplayMetrics().widthPixels;
                            int height = getResources().getDisplayMetrics().heightPixels;
                            int padding = (int) (width * 0.20); // offset from edges of the map 10% of screen

                            CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
                            googleMap.animateCamera(cu);
                            //------------------------------------------------------------------\\

                        } else if (status.equals(RequestResult.NOT_FOUND)) {
                            Toast.makeText(context, "No routes exist", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onDirectionFailure(Throwable t) {
                        // Do something here
                    }
                });
        //-------------------------------------------------------------------------------\\

    }
Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
1

there is no implementation of :

1) DistanceObject class

2) DurationObject class

public class DistanceObject{


    private Integer value;
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

same class for duration

public class DurationObject {

    private String text;

    private Integer value;


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

to get distance

Integer disInMeters=routeA.getLegs().get(0).getDistance().getValue();
int  kilometers = (int) (disInMeters * 0.001); //convert in KM

to get duration

Integer duration=routeA.getLegs().get(0).getDuration().getValue();

for more help follow this link

Android
  • 1,420
  • 4
  • 13
  • 23
Adil
  • 812
  • 1
  • 9
  • 29