Displaying multiple routes using Directions API in Android

后端 未结 3 829
说谎
说谎 2020-12-09 06:32

I am using this class to display a route on a map. The problem is that it only displays one route. What I want to do is display multiple alternate routes on the map. Even th

3条回答
  •  一个人的身影
    2020-12-09 06:57

    i think you don't have to get the response of Google Server and parse it in Document, other wise you can convert from InputStream to String using:

    private String convertStreamToString(final InputStream input) throws Exception {
        try {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            final StringBuffer sBuf = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sBuf.append(line);
            }
            return sBuf.toString();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                input.close();
            } catch (Exception e) {
                throw e;
            }
        }
    

    then you will have to parse the response as JSONObject

    JSONObject jSONObject = new JSONObject(string);
    

    then you get JSONArray named routes

    JSONArray routeJSONArray = jSONObject.getJSONArray("routes");
    

    now you can start fetching data from each route by getting its index from JSONArray.

    i have written a snippet of code as a model of route

    Route.java

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    
    import com.google.android.gms.maps.model.Polyline;
    
    public class Route implements Serializable {
    
        private static final long serialVersionUID = 1L;
        private Bound bounds;
        private String copyrights;
        private List legs;
        private Polyline overviewPolyLine;
        private String summary;
    
        public Route(Context context) {
            legs = new ArrayList();
        }
    
        public Bound getBounds() {
            return bounds;
        }
    
        public void setBounds(Bound bounds) {
            this.bounds = bounds;
        }
    
        public String getCopyrights() {
            return copyrights;
        }
    
        public void setCopyrights(String copyrights) {
            this.copyrights = copyrights;
        }
    
        public List getLegs() {
            return legs;
        }
    
        public void setLegs(List legs) {
            this.legs = legs;
        }
    
        public void addLeg(Leg leg) {
            this.legs.add(leg);
        }
    
        public Polyline getOverviewPolyLine() {
            return overviewPolyLine;
        }
    
        public void setOverviewPolyLine(Polyline overviewPolyLine) {
            this.overviewPolyLine = overviewPolyLine;
        }
    
        public String getSummary() {
            return summary;
        }
    
        public void setSummary(String summary) {
            this.summary = summary;
        }
    
        public static long getSerialversionuid() {
            return serialVersionUID;
        }
    
    }
    

    Bound.java

    import com.google.android.gms.maps.model.LatLng;
    
    public class Bound {
        private LatLng northEast;
        private LatLng southWest;
        public LatLng getNorthEast() {
            return northEast;
        }
        public void setNorthEast(LatLng northEast) {
            this.northEast = northEast;
        }
        public LatLng getSouthWest() {
            return southWest;
        }
        public void setSouthWest(LatLng southWest) {
            this.southWest = southWest;
        }
    }
    

    Leg.java

    import java.util.ArrayList;
    import java.util.List;
    
    import com.google.android.gms.maps.model.LatLng;
    
    public class Leg {
        private Distance distance;
        private Duration duration;
        private String endAddress;
        private LatLng endLocation;
        private String startAddress;
        private LatLng startLocation;
        private List steps;
    
        public Leg() {
            steps = new ArrayList();
        }
    
        public Distance getDistance() {
            return distance;
        }
    
        public void setDistance(Distance distance) {
            this.distance = distance;
        }
    
        public Duration getDuration() {
            return duration;
        }
    
        public void setDuration(Duration duration) {
            this.duration = duration;
        }
    
        public String getEndAddress() {
            return endAddress;
        }
    
        public void setEndAddress(String endAddress) {
            this.endAddress = endAddress;
        }
    
        public LatLng getEndLocation() {
            return endLocation;
        }
    
        public void setEndLocation(LatLng endLocation) {
            this.endLocation = endLocation;
        }
    
        public String getStartAddress() {
            return startAddress;
        }
    
        public void setStartAddress(String startAddress) {
            this.startAddress = startAddress;
        }
    
        public LatLng getStartLocation() {
            return startLocation;
        }
    
        public void setStartLocation(LatLng startLocation) {
            this.startLocation = startLocation;
        }
    
        public List getSteps() {
            return steps;
        }
    
        public void setSteps(List steps) {
            this.steps = steps;
        }
    
        public void addStep(Step step) {
            this.steps.add(step);
        }
    
    }
    

    Distance.java

    public class Distance {
        private String text;
        private long value;
    
        public Distance(String text, long value) {
            this.text = text;
            this.value = value;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public long getValue() {
            return value;
        }
    
        public void setValue(long value) {
            this.value = value;
        }
    
    }
    

    Duration.java

    public class Duration {
        public Duration(String text, long value) {
            this.text = text;
            this.value = value;
        }
    
        private String text;
        private long value;
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public long getValue() {
            return value;
        }
    
        public void setValue(long value) {
            this.value = value;
        }
    
    }
    

    Step.java

    import java.util.List;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    
    import com.google.android.gms.maps.model.LatLng;
    import com.nweave.etaxi.driver.R;
    
    public class Step {
        private Distance distance;
        private Duration duration;
        private LatLng endLocation;
        private LatLng startLocation;
        private String htmlInstructions;
        private String travelMode;
        private List points;
    
        public List getPoints() {
            return points;
        }
    
        public void setPoints(List points) {
            this.points = points;
        }
    
        public Distance getDistance() {
            return distance;
        }
    
        public void setDistance(Distance distance) {
            this.distance = distance;
        }
    
        public Duration getDuration() {
            return duration;
        }
    
        public void setDuration(Duration duration) {
            this.duration = duration;
        }
    
        public LatLng getEndLocation() {
            return endLocation;
        }
    
        public void setEndLocation(LatLng endLocation) {
            this.endLocation = endLocation;
        }
    
        public LatLng getStartLocation() {
            return startLocation;
        }
    
        public void setStartLocation(LatLng startLocation) {
            this.startLocation = startLocation;
        }
    
        public String getHtmlInstructions() {
            return htmlInstructions;
        }
    
        public void setHtmlInstructions(String htmlInstructions) {
            this.htmlInstructions = htmlInstructions;
        }
    
        public String getTravelMode() {
            return travelMode;
        }
    
        public void setTravelMode(String travelMode) {
            this.travelMode = travelMode;
        }
    }
    

    the parsing function will be

    public List parse(String routesJSONString) throws Exception {
        try {
            List routeList = new ArrayList();
            final JSONObject jSONObject = new JSONObject(routesJSONString);
            JSONArray routeJSONArray = jSONObject.getJSONArray(ROUTES);
            Route route;
            JSONObject routesJSONObject;
            for (int m = 0; m < routeJSONArray.length(); m++) {
                route = new Route(context);
                routesJSONObject = routeJSONArray.getJSONObject(m);
                JSONArray legsJSONArray;
                route.setSummary(routesJSONObject.getString(SUMMARY));
                legsJSONArray = routesJSONObject.getJSONArray(LEGS);
                JSONObject legJSONObject;
                Leg leg;
                JSONArray stepsJSONArray;
                for (int b = 0; b < legsJSONArray.length(); b++) {
                    leg = new Leg();
                    legJSONObject = legsJSONArray.getJSONObject(b);
                    leg.setDistance(new Distance(legJSONObject.optJSONObject(DISTANCE).optString(TEXT), legJSONObject.optJSONObject(DISTANCE).optLong(VALUE)));
                    leg.setDuration(new Duration(legJSONObject.optJSONObject(DURATION).optString(TEXT), legJSONObject.optJSONObject(DURATION).optLong(VALUE)));
                    stepsJSONArray = legJSONObject.getJSONArray(STEPS);
                    JSONObject stepJSONObject, stepDurationJSONObject, legPolyLineJSONObject, stepStartLocationJSONObject, stepEndLocationJSONObject;
                    Step step;
                    String encodedString;
                    LatLng stepStartLocationLatLng, stepEndLocationLatLng;
                    for (int i = 0; i < stepsJSONArray.length(); i++) {
                        stepJSONObject = stepsJSONArray.getJSONObject(i);
                        step = new Step();
                        JSONObject stepDistanceJSONObject = stepJSONObject.getJSONObject(DISTANCE);
                        step.setDistance(new Distance(stepDistanceJSONObject.getString(TEXT), stepDistanceJSONObject.getLong(VALUE)));
                        stepDurationJSONObject = stepJSONObject.getJSONObject(DURATION);
                        step.setDuration(new Duration(stepDurationJSONObject.getString(TEXT), stepDurationJSONObject.getLong(VALUE)));
                        stepEndLocationJSONObject = stepJSONObject.getJSONObject(END_LOCATION);
                        stepEndLocationLatLng = new LatLng(stepEndLocationJSONObject.getDouble(LATITUDE), stepEndLocationJSONObject.getDouble(LONGITUDE));
                        step.setEndLocation(stepEndLocationLatLng);
                        step.setHtmlInstructions(stepJSONObject.getString(HTML_INSTRUCTION));
                        legPolyLineJSONObject = stepJSONObject.getJSONObject(POLYLINE);
                        encodedString = legPolyLineJSONObject.getString(POINTS);
                        step.setPoints(decodePolyLines(encodedString));
                        stepStartLocationJSONObject = stepJSONObject.getJSONObject(START_LOCATION);
                        stepStartLocationLatLng = new LatLng(stepStartLocationJSONObject.getDouble(LATITUDE), stepStartLocationJSONObject.getDouble(LONGITUDE));
                        step.setStartLocation(stepStartLocationLatLng);
                        leg.addStep(step);
                    }
                    route.addLeg(leg);
                }
                routeList.add(route);
            }
            return routeList;
        } catch (Exception e) {
            throw e;
        }
    

    Regarding the Step Image there is an HTML instruction and another field called maneuver where according to this field you will choose your image

    i hope this helps ;)

提交回复
热议问题