[ADD] start of api calls
This commit is contained in:
@@ -33,8 +33,8 @@ public class Location {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
public Location(@NotNull String name, double startLat, double startLong, double endLat, double endLong, String description, @Nullable String imageUrl) {
|
||||
this(name,getStringFromCoordinates(startLat,startLong,endLat,endLong),description,imageUrl);
|
||||
public Location(@NotNull String name, double latCoord, double longCoord, String description, @Nullable String imageUrl) {
|
||||
this(name,getStringFromCoordinates(latCoord,longCoord),description,imageUrl);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -72,18 +72,23 @@ public class Location {
|
||||
}
|
||||
|
||||
public double[] getCoordinatesAsDoubles() {
|
||||
double[] res = new double[4];
|
||||
String[] locations = this.coordinates.split(";");
|
||||
res[0] =Double.parseDouble(locations[0].split(",")[0]);
|
||||
res[1] = Double.parseDouble(locations[0].split(",")[1]);
|
||||
res[2] = Double.parseDouble(locations[1].split(",")[0]);
|
||||
res[3] = Double.parseDouble(locations[1].split(",")[1]);
|
||||
double[] res = new double[2];
|
||||
res[0] = getLat();
|
||||
res[1] = getLong();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static String getStringFromCoordinates(double lat1, double long1, double lat2, double long2) {
|
||||
return lat1 + "," + long1 + ";" + lat2 + "," + long2;
|
||||
public double getLat() {
|
||||
return Double.parseDouble(this.coordinates.split(",")[0]);
|
||||
}
|
||||
|
||||
public double getLong() {
|
||||
return Double.parseDouble(this.coordinates.split(",")[1]);
|
||||
}
|
||||
|
||||
public static String getStringFromCoordinates(double lat1, double long1) {
|
||||
return lat1 + "," + long1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,14 @@ package com.a1.nextlocation.network;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public enum ApiHandler {
|
||||
INSTANCE;
|
||||
@@ -12,11 +18,46 @@ public enum ApiHandler {
|
||||
|
||||
private static String TAG = ApiHandler.class.getCanonicalName();
|
||||
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
private final String BASE_URL = "https://api.openrouteservice.org/v2/directions/";
|
||||
private final String API_KEY = "5b3ce3597851110001cf6248d4eee2099f724255918adc71cc502b2a";
|
||||
private final String DIRECTIONS_MODE = "foot_walking";
|
||||
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
|
||||
public Route getDirections(Location startLocation, Location endLocation) {
|
||||
return null;
|
||||
return getDirections(startLocation.getCoordinates(),endLocation.getCoordinates());
|
||||
}
|
||||
|
||||
public Route getDirections(double startLat, double startLong, double endLat, double endLong) {
|
||||
return getDirections(startLat + "," + startLong, endLat + "," + endLong);
|
||||
}
|
||||
|
||||
public Route getDirections(String startLocation, String endLocation) {
|
||||
String requestUrl = BASE_URL + DIRECTIONS_MODE + "?api_key=" + API_KEY + "&start=" +startLocation + "&end=" + endLocation;
|
||||
AtomicReference<Route> res = null;
|
||||
Thread t = new Thread(() -> {
|
||||
|
||||
Request request = new Request.Builder().url(requestUrl).build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.body() != null) {
|
||||
String responseString = Objects.requireNonNull(response.body()).string();
|
||||
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user