对谷歌路径规划解析获取最优路径

来源:互联网 发布:像素软件招聘 编辑:程序博客网 时间:2024/05/21 06:19

解析用到的实体


public class DirectionModel {

    private String status;
    private List<GeocodedWaypoints> geocoded_waypoints;
    private List<Routes> routes;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<GeocodedWaypoints> getGeocoded_waypoints() {
        return geocoded_waypoints;
    }

    public void setGeocoded_waypoints(List<GeocodedWaypoints> geocoded_waypoints) {
        this.geocoded_waypoints = geocoded_waypoints;
    }

    public List<Routes> getRoutes() {
        return routes;
    }

    public void setRoutes(List<Routes> routes) {
        this.routes = routes;
    }

    public DirectionModel() {

    }

    class GeocodedWaypoints {
        private String geocoder_status;
        private String place_id;
        private List<String> types;

        public String getGeocoder_status() {
            return geocoder_status;
        }

        public void setGeocoder_status(String geocoder_status) {
            this.geocoder_status = geocoder_status;
        }

        public String getPlace_id() {
            return place_id;
        }

        public void setPlace_id(String place_id) {
            this.place_id = place_id;
        }

        public List<String> getTypes() {
            return types;
        }

        public void setTypes(List<String> types) {
            this.types = types;
        }

    }

    public class Routes {

        private List<Legs> legs;
        private Fare fare;

        public List<Legs> getLegs() {
            return legs;
        }

        public void setLegs(List<Legs> legs) {
            this.legs = legs;
        }

        public Fare getFare() {
            return fare;
        }

        public void setFare(Fare fare) {
            this.fare = fare;
        }

        public class Legs {

            private Distance distance;
            private Duration duration;
            private String start_address;
            private String end_address;
            private StartLocation start_location;
            private EndLocation end_location;
            private List<Steps> steps;

            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 getStart_address() {
                return start_address;
            }

            public void setStart_address(String start_address) {
                this.start_address = start_address;
            }

            public String getEnd_address() {
                return end_address;
            }

            public void setEnd_address(String end_address) {
                this.end_address = end_address;
            }

            public StartLocation getStart_location() {
                return start_location;
            }

            public void setStart_location(StartLocation start_location) {
                this.start_location = start_location;
            }

            public EndLocation getEnd_location() {
                return end_location;
            }

            public void setEnd_location(EndLocation end_location) {
                this.end_location = end_location;
            }

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

            public void setSteps(List<Steps> steps) {
                this.steps = steps;
            }

            public class Distance {
                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;
                }

            }

            public class Duration {
                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;
                }

            }

            public class StartLocation {
                private double lat;
                private double lng;

                public double getLat() {
                    return lat;
                }

                public void setLat(double lat) {
                    this.lat = lat;
                }

                public double getLng() {
                    return lng;
                }

                public void setLng(double lng) {
                    this.lng = lng;
                }

            }

            public class EndLocation {
                private double lat;
                private double lng;

                public double getLat() {
                    return lat;
                }

                public void setLat(double lat) {
                    this.lat = lat;
                }

                public double getLng() {
                    return lng;
                }

                public void setLng(double lng) {
                    this.lng = lng;
                }

            }

            public class Steps {
                private StartLocation start_location;
                private EndLocation end_location;
                private String html_instructions;
                private List<Steps> steps;

                public StartLocation getStart_location() {
                    return start_location;
                }

                public void setStart_location(StartLocation start_location) {
                    this.start_location = start_location;
                }

                public EndLocation getEnd_location() {
                    return end_location;
                }

                public void setEnd_location(EndLocation end_location) {
                    this.end_location = end_location;
                }

                public String getHtml_instructions() {
                    return html_instructions.replace(" ", "").substring(3);
                }

                public void setHtml_instructions(String html_instructions) {
                    this.html_instructions = html_instructions;
                }

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

                public void setSteps(List<Steps> steps) {
                    this.steps = steps;
                }

                @Override
                public String toString() {
                    return "Steps [start_location=" + start_location
                            + ", end_location=" + end_location
                            + ", html_instructions=" + html_instructions
                            + ", steps=" + steps + "]";
                }
            }
        }

        public class Fare {
            private String currency;
            private int value;
            private String text;

            public String getCurrency() {
                return currency;
            }

            public void setCurrency(String currency) {
                this.currency = currency;
            }

            public int getValue() {
                return value;
            }

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

            public String getText() {
                return text;
            }

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

将结果转换为自己需要的实体

public class BestRouteModel {

    private long distance;//距离
    private double fare;//费用,不经过收费站就为0
    private String visit;//经过地点的顺序,格式为A->B->C

    public BestRouteModel() {

    }

    public long getDistance() {
        return distance;
    }

    public void setDistance(long distance) {
        this.distance = distance;
    }

    public double getFare() {
        return fare;
    }

    public void setFare(double fare) {
        this.fare = fare;
    }

    public String getVisit() {
        return visit;
    }

    public void setVisit(String visit) {
        this.visit = visit;
    }
}


比较器,用于对路径结果排序,距离小的在上,其次比费用


public class MyComparator implements Comparator<BestRouteModel> {

    public MyComparator() {
    }

    @Override
    public int compare(BestRouteModel lhs, BestRouteModel rhs) {

        if (lhs != null && rhs != null) {
            if (lhs.getDistance() > rhs.getDistance()) {
                return 1;
            } else if (lhs.getDistance() == rhs.getDistance()) {
                if (lhs.getFare() > rhs.getFare()) {
                    return 1;
                }
            }
        }
        return -1;
    }
}


请求使用的URL地址

origin:起点,

destination 终点

waypoints 途经点

三个参数可以是地名可以是坐标,坐标用逗号隔开

key 可以没有

String path = Comment.ROUTE_PLAN_URL
                                    + "origin="
                                    + origin
                                    + "&destination="
                                    + des
                                    + "&waypoints=optimize:true|"
                                    + wayPoints.toString()

                                    + "&alternatives=true"                                   

                                    + "&key=添加自己的key";
 

获取最优路径

// get best route from route list
    private BestRouteModel getBestRoute(DirectionModel route) {
        BestRouteModel bestRoute = new BestRouteModel();
        List<BestRouteModel> routePlanList = new ArrayList<>();
        if (route != null) {
            // get all routes
            List<Routes> routesList = route.getRoutes();
            if (routesList != null && routesList.size() > 0) {
                for (Routes routes : routesList) {
                    BestRouteModel r = new BestRouteModel();
                    if (routes != null) {
                        // get fare
                        Fare fare = routes.getFare();
                        if (fare != null) {
                            r.setFare(fare.getValue());
                        }
                        // get all legs
                        List<Legs> legsList = routes.getLegs();
                        if (legsList != null && legsList.size() > 0) {
                            StringBuilder sb = new StringBuilder();
                            sb.append(legsList.get(0).getStart_address());
                            long distance = 0;
                            for (Legs leg : legsList) {
                                Distance d = leg.getDistance();
                                if (d != null) {
                                    distance += d.getValue();
                                }
                                String end = leg.getEnd_address();

                                if (!TextUtils.isEmpty(end)) {
                                    sb.append("->").append(end);
                                }
                            }
                            r.setDistance(distance);
                            r.setVisit(sb.toString());
                            routePlanList.add(r);
                        }
                    }
                }
                Collections.sort(routePlanList, new MyComparator());
                bestRoute = routePlanList.get(0);
            }
        }
        return bestRoute;
    }

0 0