对谷歌路径规划解析获取最优路径
来源:互联网 发布:像素软件招聘 编辑:程序博客网 时间: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;
}
- 对谷歌路径规划解析获取最优路径
- pgrouting最优路径规划之一
- 动态规划-poj1088 滑雪 ---增加了最优路径构建
- 基于WSN的灾难现场最优逃生路径规划
- noip2008 传纸条 (动态规划寻找最优路径)
- 数塔类动态规划的最优路径的输出
- 动态规划-点数值三角形的最优路径搜索 java
- pgrouting两个经纬度点之间最优路径规划
- 路径规划算法的完备性与概率完备性、最优性与渐进最优性
- 动态规划——求数字三角形最优解和最优路径
- 【动态规划】最优时间表
- 动态规划-最优解
- 动态规划--最优编辑
- 动态规划-最优编辑
- 0-1背包问题,poj 3624 Charm Bracelet动态规划-解题报告,增加最优路径构建
- 【动态规划】求二维数组从左下到右上的最优路径
- 算法:动态规划-数字三角形的最优解(打印路径)
- C的最优路径
- Java对Map排序
- leetcode86. Partition List
- Apple Pay接入详细教程
- Linux LVM工具(VG和LV)
- Android Studio使用AIDL 实现进程间通信实例
- 对谷歌路径规划解析获取最优路径
- 1366*768 显示屏模式造成远程桌面不能全屏显示
- Unix系统下进程间通信方式及比较
- C#关于FTP上的文件操作
- 继承自UITableViewController 改为继承自viewController
- windows+apache+php+mysql快速搭建服务器环境
- java多线程并发去调用一个类的静态方法安全性探讨
- ScrollView中嵌套 GridView 导致 ScrollView默认不停留在顶部的解决方案和分析
- 【C++11新特性】 C++11智能指针之shared_ptr