Get Mapquest Directions -> Strecke, Dauer

This commit is contained in:
Georg Reisinger 2021-04-16 17:07:49 +02:00
parent 9fe6f34ba3
commit 93cb83e33e
6 changed files with 98 additions and 5 deletions

View File

@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="com.google.code.gson:gson:2.8.6" type="repository">
<properties maven-id="com.google.code.gson:gson:2.8.6" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.6/gson-2.8.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -12,5 +12,7 @@
<orderEntry type="library" name="log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="org.postgresql:postgresql:42.2.19" level="project" />
<orderEntry type="library" name="iText-4.2.0-com.itextpdf" level="project" />
<orderEntry type="library" name="org.json:json:20180130" level="project" />
<orderEntry type="library" name="com.google.code.gson:gson:2.8.6" level="project" />
</component>
</module>

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}