From 184234949ff36d59aa46570f31e995d9422683ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E7=90=B3?= <1911390090@qq.com> Date: Mon, 1 Jul 2024 09:50:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A5=E5=A0=B5=E8=B7=AF=E6=AE=B5=20?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB=E8=AE=A1=E7=AE=97(=E4=BF=AE=E6=94=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DcTrafficStatisticsController.java | 2 +- .../service/IDcTrafficStatisticsService.java | 2 +- .../impl/DcTrafficStatisticsServiceImpl.java | 82 ++++++++++--------- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java index 4f1f0f8c..2558b1a3 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcTrafficStatisticsController.java @@ -576,7 +576,7 @@ public AjaxResult realTimeTrafficFlow(String startDate, String direction,String @GetMapping("/current/getTheCurrentCongestedSection") public AjaxResult getTheCurrentCongestedSection() throws HttpException, IOException { // 调用服务层方法,获取当前交通指标数据 - JSONObject jsonArray = dcTrafficStatisticsService.getTheCurrentCongestedSection(); + JSONArray jsonArray = dcTrafficStatisticsService.getTheCurrentCongestedSection(); // 将获取到的交通指标数据封装为成功的结果并返回 return AjaxResult.success(jsonArray); } diff --git a/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java b/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java index 2ffe7473..b9f99d05 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcTrafficStatisticsService.java @@ -56,6 +56,6 @@ public interface IDcTrafficStatisticsService { Map>> realTimeTrafficFlowHour() throws HttpException, IOException; List> queryTheGantryDataByPileNumber(String startDate,String stakeMark) throws HttpException, IOException; - JSONObject getTheCurrentCongestedSection()throws HttpException, IOException; + JSONArray getTheCurrentCongestedSection()throws HttpException, IOException; } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java index 206c43f3..d0a982c6 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java @@ -907,7 +907,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi * @throws IOException */ @Override - public JSONObject getTheCurrentCongestedSection() throws HttpException, IOException { + public JSONArray getTheCurrentCongestedSection() throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); @@ -927,49 +927,51 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi ResponseBody body = response.body(); if (body != null) { JSONArray jsonArray = JSON.parseArray(body.string()); - JSONObject jsonObject = (JSONObject) jsonArray.get(0); - Integer asOneRoad = jsonObject.getInteger("as_one_road"); - - if (asOneRoad == UniversalEnum.ONE.getNumber()){ - - String string = jsonObject.getString("list_link_info"); - JSONArray jsonlist = JSON.parseArray(string); - JSONObject o = (JSONObject) jsonlist.get(0); - - String string1 = o.getString("firstPoint"); - String string2 = o.getString("lastPoint"); - String lngLats = o.getString("lngLats"); - String[] lngLatsString = lngLats.split(";"); - double s = Double.parseDouble(lngLatsString[0].split(",")[0]); - double s2 = Double.parseDouble(lngLatsString[0].split(",")[1]); - double s3 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[0]); - double s4 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[1]); - double distanceM = calculateDistance(s2, s, s4, s3)* 1000; - // System.out.printf("最大距离为: %.2f 米%n", distanceM); - jsonObject.put("max_jam_dist",distanceM);//最大拥堵距离 - String[] split = string1.split(","); - double firstLon = Double.parseDouble(split[0]); - double firstLat = Double.parseDouble(split[1]); - String[] split2 = string2.split(","); - double lastLon = Double.parseDouble(split2[0]); - double lastLat = Double.parseDouble(split2[1]); - //double firstLat = 35.38524527319016; - // double firstLon = 118.39808642864227; - //double lastLat = 35.386351346969604; - // double lastLon = 118.4038907289505; - double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon); - double distanceMeters = distanceKm * 1000; // 将距离转换为米 - //System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters); - jsonObject.put("jam_dist",distanceMeters);//当前拥堵距离 - - return jsonObject; - }else { + for (Object object : jsonArray) { + JSONObject jsonObject = (JSONObject) object; + Integer asOneRoad = jsonObject.getInteger("as_one_road"); + //as_one_road + //是否为同一条路,1-是,0-否 + if (asOneRoad == UniversalEnum.ZERO.getNumber()){ + + String string = jsonObject.getString("list_link_info"); + JSONArray jsonlist = JSON.parseArray(string); + JSONObject o = (JSONObject) jsonlist.get(0); + + String string1 = o.getString("firstPoint"); + String string2 = o.getString("lastPoint"); + String lngLats = o.getString("lngLats"); + String[] lngLatsString = lngLats.split(";"); + double s = Double.parseDouble(lngLatsString[0].split(",")[0]); + double s2 = Double.parseDouble(lngLatsString[0].split(",")[1]); + double s3 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[0]); + double s4 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[1]); + double distanceM = calculateDistance(s2, s, s4, s3)* 1000; + // System.out.printf("最大距离为: %.2f 米%n", distanceM); + jsonObject.put("max_jam_dist",distanceM);//最大拥堵距离 + String[] split = string1.split(","); + double firstLon = Double.parseDouble(split[0]); + double firstLat = Double.parseDouble(split[1]); + String[] split2 = string2.split(","); + double lastLon = Double.parseDouble(split2[0]); + double lastLat = Double.parseDouble(split2[1]); + //double firstLat = 35.38524527319016; + // double firstLon = 118.39808642864227; + //double lastLat = 35.386351346969604; + // double lastLon = 118.4038907289505; + double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon); + double distanceMeters = distanceKm * 1000; // 将距离转换为米 + //System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters); + jsonObject.put("jam_dist",distanceMeters);//当前拥堵距离 + + } } + return jsonArray; } - return new JSONObject(); + return new JSONArray(); } @@ -1019,6 +1021,8 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi // 计算两点间的距离 double distance = EARTH_RADIUS * c; + // 保留两位小数,四舍五入 + distance= Double.parseDouble(String.format("%.5f", distance)); return distance; }