为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第17天 多端全栈项目实战

标签:
SpringBoot

课程名称:多端全栈项目实战:商业级代驾全流程落地


课程章节:  华夏代驾全栈小程序实战


课程讲师: 神思者


课程内容:


        前端实时上传gps定位


课程收获:

        

        当上传了实时得定位坐标,并且在redis里面保存了司机得geo缓存和上线缓存,接下来就是创建订单业务,在创建订单得过程中,需要先查找附近适合接单得司机,如果有司机,代驾系统才去创建订单,否则就不创建。




https://img1.sycdn.imooc.com//632ab9f400014ff608050278.jpg

因为要计算方圆几公里内得司机,我们就要用到redis 得 geo 计算


首先编写一个抽象方法

public ArrayList searchBefittingDriverAboutOrder(double startPlaceLatitude, 
                                                     double startPlaceLongitude,
                                                     double endPlaceLatitude, 
                                                     double endPlaceLongitude, 
                                                     double mileage);


分别是 起点左边 终点坐标 在一个是 代驾里程 


然后是 抽象方法得实现


首先 搜索范围5公里内得司机

Point point = new Point(startPlaceLongitude, startPlaceLatitude);
//设置GEO距离单位为千米
Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
Distance distance = new Distance(5, metric);
Circle circle = new Circle(point, distance);

//创建GEO参数
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs                .newGeoRadiusArgs()
     .includeDistance() //结果中包含距离
     .includeCoordinates() //结果中包含坐标
     .sortAscending(); //升序排列

//执行GEO计算,获得查询结果
GeoResults<RedisGeoCommands.GeoLocation<String>> radius = redisTemplate.opsForGeo()
                .radius("driver_location", circle, args);


查找司机是否在线

//排查掉不在线的司机
if (!redisTemplate.hasKey("driver_online#" + driverId)) {
    continue;
}
//查找该司机的在线缓存
Object obj = redisTemplate.opsForValue().get("driver_online#" + driverId);
//如果查找的那一刻,缓存超时被置空,那么就忽略该司机
if (obj == null) {
      continue;
}

判断两次是因为 上一秒还在 但马上下线得情况


判断是否符合接单范围

//判断订单里程是否符合
 boolean bool_2 = false;
if (orderDistance == 0) {
    bool_2 = true;
} else if (orderDistance == 5 && mileage > 0 && mileage <= 5) {
    bool_2 = true;
} else if (orderDistance == 10 && mileage > 5 && mileage <= 10) {
    bool_2 = true;
} else if (orderDistance == 15 && mileage > 10 && mileage <= 15) {
    bool_2 = true;
} else if (orderDistance == 30 && mileage > 15 && mileage <= 30) {
    bool_2 = true;
}



判断订单和司机定向是否符合

//判断定向接单是否符合
boolean bool_3 = false;
if (!orientation.equals("none")) {
    double orientationLatitude = Double.parseDouble(orientation.split(",")[0]);
    double orientationLongitude = Double.parseDouble(orientation.split(",")[1]);
    //把定向点的火星坐标转换成GPS坐标
    double[] location = CoordinateTransform.transformGCJ02ToWGS84(orientationLongitude, orientationLatitude);
    GlobalCoordinates point_1 = new GlobalCoordinates(location[1], location[0]);
    //把订单终点的火星坐标转换成GPS坐标
    location = CoordinateTransform.transformGCJ02ToWGS84(endPlaceLongitude, endPlaceLatitude);
    GlobalCoordinates point_2 = new GlobalCoordinates(location[1], location[0]);
    //这里不需要Redis的GEO计算,直接用封装函数计算两个GPS坐标之间的距离
    GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, point_1, point_2);

    //如果定向点距离订单终点距离在3公里以内,说明这个订单和司机定向点是顺路的
        if (geoCurve.getEllipsoidalDistance() <= 3000) {
              bool_3 = true;
        }

    } else {
     bool_3 = true;
}



        https://img1.sycdn.imooc.com//632ab9ad00013ec408300742.jpg

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
25
获赞与收藏
19

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消