From 93cb83e33e43378c7a79a34fd568eb76b432b5f3 Mon Sep 17 00:00:00 2001 From: Georg-Notebook Date: Fri, 16 Apr 2021 17:07:49 +0200 Subject: [PATCH] Get Mapquest Directions -> Strecke, Dauer --- .../com_google_code_gson_gson_2_8_6.xml | 10 +++++ Tourplaner.iml | 2 + src/tourplaner/business/DirectionMap.java | 24 +++++++++++- src/tourplaner/business/HttpHelper.java | 39 ++++++++++++++++++- src/tourplaner/business/JsonHelper.java | 20 ++++++++++ src/tourplaner/business/TourPlaner.java | 8 +++- 6 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 .idea/libraries/com_google_code_gson_gson_2_8_6.xml create mode 100644 src/tourplaner/business/JsonHelper.java diff --git a/.idea/libraries/com_google_code_gson_gson_2_8_6.xml b/.idea/libraries/com_google_code_gson_gson_2_8_6.xml new file mode 100644 index 0000000..da0c79d --- /dev/null +++ b/.idea/libraries/com_google_code_gson_gson_2_8_6.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Tourplaner.iml b/Tourplaner.iml index edd667f..860c48a 100644 --- a/Tourplaner.iml +++ b/Tourplaner.iml @@ -12,5 +12,7 @@ + + \ No newline at end of file diff --git a/src/tourplaner/business/DirectionMap.java b/src/tourplaner/business/DirectionMap.java index 820002b..7073517 100644 --- a/src/tourplaner/business/DirectionMap.java +++ b/src/tourplaner/business/DirectionMap.java @@ -31,9 +31,18 @@ public class DirectionMap { this.filepdf = ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "path") + this.tourname + "_pdf.jpg"; FileHelper.saveImage(this.map, "jpg", new File(this.file)); FileHelper.saveImage(this.mappdf, "jpg", new File(this.filepdf)); + getDirections(start, ende); FileHelper.openDefault(file); } + public double getDauer() { + return dauer; + } + + public double getStrecke() { + return strecke; + } + /** * Downloadet die Map von Mapquest * @param start Startpunkt @@ -45,7 +54,18 @@ public class DirectionMap { return HttpHelper.httpGetImage("https://www.mapquestapi.com/staticmap/v5/map?start="+start+"&end="+ende+"&size="+size+"&key="+ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "key")); } + private void getDirections(String start, String ende) throws IOException { + String json = HttpHelper.httpGetJsonString("https://www.mapquestapi.com/directions/v2/route?key="+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "key")+"&from="+start+"&to="+ende+"&outFormat=json&ambiguities=ignore&routeType=fastest&doReverseGeocode=false&enhancedNarrative=false&avoidTimedConditions=false"); + this.strecke = JsonHelper.getDoubleFromJson(json, "distance"); + this.dauer = formatetTimeToMinutes(JsonHelper.getStingFromJson(json, "formattedTime")); + } - - + private double formatetTimeToMinutes(String formatetTime){ + double minutes = 0; + String[] result = formatetTime.split(":"); + minutes += Double.parseDouble(result[0]) * 60; + minutes += Double.parseDouble(result[1]); + minutes += Double.parseDouble(result[2]) / 60; + return minutes; + } } diff --git a/src/tourplaner/business/HttpHelper.java b/src/tourplaner/business/HttpHelper.java index 8769813..0695d48 100644 --- a/src/tourplaner/business/HttpHelper.java +++ b/src/tourplaner/business/HttpHelper.java @@ -1,8 +1,11 @@ package tourplaner.business; + + import javax.imageio.ImageIO; import java.awt.*; -import java.io.IOException; +import java.io.*; +import java.net.HttpURLConnection; import java.net.URL; /** @@ -20,4 +23,38 @@ public class HttpHelper { URL urls = new URL(url); return ImageIO.read(urls); } + + public static String httpGetJsonString(String url) throws IOException { + HttpURLConnection con = null; + try { + + var myurl = new URL(url); + con = (HttpURLConnection) myurl.openConnection(); + + con.setRequestMethod("GET"); + + StringBuilder content; + + try (BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream()))) { + + String line; + content = new StringBuilder(); + + while ((line = in.readLine()) != null) { + + content.append(line); + content.append(System.lineSeparator()); + } + } + + return content.toString(); + } finally { + + assert con != null; + con.disconnect(); + } + } + + } diff --git a/src/tourplaner/business/JsonHelper.java b/src/tourplaner/business/JsonHelper.java new file mode 100644 index 0000000..bc5afab --- /dev/null +++ b/src/tourplaner/business/JsonHelper.java @@ -0,0 +1,20 @@ +package tourplaner.business; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +public class JsonHelper { + + public static String getStingFromJson(String json, String gets){ + return getJObj(json).get("route").getAsJsonObject().get(gets).getAsString(); + } + + public static double getDoubleFromJson(String json, String gets){ + return getJObj(json).get("route").getAsJsonObject().get(gets).getAsDouble(); + } + + private static JsonObject getJObj(String json){ + return new Gson().fromJson(json, JsonObject.class); + } +} diff --git a/src/tourplaner/business/TourPlaner.java b/src/tourplaner/business/TourPlaner.java index 302744b..2d9fcad 100644 --- a/src/tourplaner/business/TourPlaner.java +++ b/src/tourplaner/business/TourPlaner.java @@ -40,7 +40,9 @@ public class TourPlaner{ public static boolean editTour(String oldname, Tour tour) throws IOException { FileHelper.delFile(new File(getImagePath(oldname))); FileHelper.delFile(new File(getImagePdfPath(oldname))); - new DirectionMap(tour.getStart(), tour.getZiel(), tour.getName()); + DirectionMap directionMap = new DirectionMap(tour.getStart(), tour.getZiel(), tour.getName()); + tour.setDauer(directionMap.getDauer()+""); + tour.setStrecke(directionMap.getStrecke()); return new DbConnect().editTour(oldname, tour); } @@ -55,7 +57,9 @@ public class TourPlaner{ * @return false bei error */ public static boolean addTour(Tour newTour) throws IOException { - new DirectionMap(newTour.getStart(), newTour.getZiel(), newTour.getName()); + DirectionMap directionMap = new DirectionMap(newTour.getStart(), newTour.getZiel(), newTour.getName()); + newTour.setDauer(directionMap.getDauer()+""); + newTour.setStrecke(directionMap.getStrecke()); return new DbConnect().addTour(newTour); }