课程名称:多端全栈项目实战:商业级代驾全流程落地
课程章节: 华夏代驾全栈小程序实战
课程讲师: 神思者
课程内容:
开通腾讯云地图 调用接口
课程收获:
昨天写了接口 接上回
接下来需要编写 controller 里面得form类
@Data@Schema(description = "预估里程和时间的表单")public class EstimateOrderMileageAndMinuteForm {
@NotBlank(message = "mode不能为空")
@Pattern(regexp = "^driving$|^walking$|^bicycling$")
@Schema(description = "计算方式")
private String mode;
@NotBlank(message = "startPlaceLatitude不能为空")
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "startPlaceLatitude内容不正确")
@Schema(description = "订单起点的纬度")
private String startPlaceLatitude;
@NotBlank(message = "startPlaceLongitude不能为空")
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "startPlaceLongitude内容不正确")
@Schema(description = "订单起点的经度")
private String startPlaceLongitude;
@NotBlank(message = "endPlaceLatitude不能为空")
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "endPlaceLatitude内容不正确")
@Schema(description = "订单终点的纬度")
private String endPlaceLatitude;
@NotBlank(message = "endPlaceLongitude不能为空")
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "endPlaceLongitude内容不正确")
@Schema(description = "订单起点的经度")
private String endPlaceLongitude;}
然后是 mapController
接收前端接口发送过来得5个值 起点坐标 重点坐标 和 计算方式
public R estimateOrderMileageAndMinute(@RequestBody @Valid EstimateOrderMileageAndMinuteForm form) {
HashMap map = mapService.estimateOrderMileageAndMinute(form.getMode(),
form.getStartPlaceLatitude(), form.getStartPlaceLongitude(),
form.getEndPlaceLatitude(), form.getEndPlaceLongitude());
return R.ok().put("result", map);
}接下来是 封装查询最佳线路
编写一个service 分别接收 起点和终点得经纬度
HttpRequest req = new HttpRequest(directionUrl);
req.form("from", startPlaceLatitude + "," + startPlaceLongitude);
req.form("to", endPlaceLatitude + "," + endPlaceLongitude);
req.form("key", key);
HttpResponse resp = req.execute();
JSONObject json = JSONUtil.parseObj(resp.body());
int status = json.getInt("status");
if (status != 0) {
throw new HxdsException("执行异常");
}
JSONObject result = json.getJSONObject("result");
HashMap map = result.toBean(HashMap.class);
return map;在这里做了异常得判断 如果status 不为零 则说明我们传的值有问题 需要抛出异常
同样的 需要编写一个form 类对 传输得值进行校验
@Data@Schema(description = "计算行驶路线的表单")public class CalculateDriveLineForm {
@NotBlank(message = "startPlaceLatitude不能为空")
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "startPlaceLatitude内容不正确")
@Schema(description = "订单起点的纬度")
private String startPlaceLatitude;
@NotBlank(message = "startPlaceLongitude不能为空")
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "startPlaceLongitude内容不正确")
@Schema(description = "订单起点的经度")
private String startPlaceLongitude;
@NotBlank(message = "endPlaceLatitude不能为空")
@Pattern(regexp = "^(([1-8]\\d?)|([1-8]\\d))(\\.\\d{1,18})|90|0(\\.\\d{1,18})?$", message = "endPlaceLatitude内容不正确")
@Schema(description = "订单终点的纬度")
private String endPlaceLatitude;
@NotBlank(message = "endPlaceLongitude不能为空")
@Pattern(regexp = "^(([1-9]\\d?)|(1[0-7]\\d))(\\.\\d{1,18})|180|0(\\.\\d{1,18})?$", message = "endPlaceLongitude内容不正确")
@Schema(description = "订单起点的经度")
private String endPlaceLongitude;}之后编写conrtoller
public R calculateDriveLine(@RequestBody @Valid CalculateDriveLineForm form) {
HashMap map = mapService.calculateDriveLine(form.getStartPlaceLatitude(), form.getStartPlaceLongitude(), form.getEndPlaceLatitude(), form.getEndPlaceLongitude());
return R.ok().put("result", map);
}就可以进行计算行驶路线得使用啦