added parsing of directions response
This commit is contained in:
@@ -13,6 +13,7 @@ import com.a1.nextlocation.fragments.HomeFragment;
|
||||
import com.a1.nextlocation.fragments.RouteFragment;
|
||||
import com.a1.nextlocation.fragments.SettingsFragment;
|
||||
import com.a1.nextlocation.fragments.StatisticFragment;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
import com.a1.nextlocation.recyclerview.CouponListManager;
|
||||
import com.a1.nextlocation.recyclerview.LocationListManager;
|
||||
import com.a1.nextlocation.recyclerview.RouteListManager;
|
||||
|
||||
@@ -110,8 +110,6 @@ public class HomeFragment extends Fragment {
|
||||
|
||||
mapView.getOverlays().add(customOverlay);
|
||||
|
||||
|
||||
|
||||
// add the zoom controller
|
||||
IMapController mapController = mapView.getController();
|
||||
mapController.setZoom(15.0);
|
||||
|
||||
@@ -2,19 +2,27 @@ package com.a1.nextlocation.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
import com.a1.nextlocation.network.DirectionsListener;
|
||||
|
||||
public class RouteFragment extends Fragment {
|
||||
private static final String TAG = RouteFragment.class.getCanonicalName();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
ApiHandler.INSTANCE.addListener(this::onDirectionsAvailable);
|
||||
|
||||
}
|
||||
|
||||
@@ -24,4 +32,15 @@ public class RouteFragment extends Fragment {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_route, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
ApiHandler.INSTANCE.getDirections(8.681436,49.41461,8.687872,49.420318);
|
||||
}
|
||||
|
||||
public void onDirectionsAvailable(DirectionsResult result) {
|
||||
Log.d(TAG, "onDirectionsAvailable: got result! " + result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.a1.nextlocation.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DirectionsResult {
|
||||
private List<DirectionsStep> steps = new ArrayList<>();
|
||||
private double distance;
|
||||
private double duration;
|
||||
|
||||
public List<DirectionsStep> getSteps() {
|
||||
return steps;
|
||||
}
|
||||
|
||||
public void setSteps(List<DirectionsStep> steps) {
|
||||
this.steps = steps;
|
||||
}
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public double getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(double duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public void addStep(DirectionsStep step) {
|
||||
this.steps.add(step);
|
||||
}
|
||||
|
||||
public void parse(String json) {
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
JsonObject features = JsonParser.parseString(json).getAsJsonObject().get("features").getAsJsonArray().get(0).getAsJsonObject();
|
||||
JsonObject segment = features.get("properties").getAsJsonObject().getAsJsonArray("segments").get(0).getAsJsonObject();
|
||||
|
||||
setDistance(segment.get("distance").getAsDouble());
|
||||
setDuration(segment.get("duration").getAsDouble());
|
||||
|
||||
JsonArray steps = segment.getAsJsonArray("steps");
|
||||
|
||||
for (JsonElement j : steps) {
|
||||
DirectionsStep step = gson.fromJson(j,DirectionsStep.class);
|
||||
addStep(step);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.a1.nextlocation.json;
|
||||
|
||||
public class DirectionsStep {
|
||||
private double distance;
|
||||
private double duration;
|
||||
private String instruction;
|
||||
private String name;
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public double getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(double duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getInstruction() {
|
||||
return instruction;
|
||||
}
|
||||
|
||||
public void setInstruction(String instruction) {
|
||||
this.instruction = instruction;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,11 @@ import android.util.Log;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@@ -22,21 +25,22 @@ public enum ApiHandler {
|
||||
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 final String DIRECTIONS_MODE = "foot-walking";
|
||||
private List<DirectionsListener> listeners = new ArrayList<>();
|
||||
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
|
||||
public Route getDirections(Location startLocation, Location endLocation) {
|
||||
return getDirections(startLocation.getCoordinates(),endLocation.getCoordinates());
|
||||
public void getDirections(Location startLocation, Location endLocation) {
|
||||
getDirections(startLocation.getCoordinates(),endLocation.getCoordinates());
|
||||
}
|
||||
|
||||
public Route getDirections(double startLat, double startLong, double endLat, double endLong) {
|
||||
return getDirections(startLat + "," + startLong, endLat + "," + endLong);
|
||||
public void getDirections(double startLat, double startLong, double endLat, double endLong) {
|
||||
getDirections(startLat + "," + startLong, endLat + "," + endLong);
|
||||
}
|
||||
|
||||
public Route getDirections(String startLocation, String endLocation) {
|
||||
public void 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();
|
||||
@@ -45,6 +49,14 @@ public enum ApiHandler {
|
||||
if (response.body() != null) {
|
||||
String responseString = Objects.requireNonNull(response.body()).string();
|
||||
Log.d(TAG, "getDirections: got response: " + responseString);
|
||||
|
||||
DirectionsResult result = new DirectionsResult();
|
||||
result.parse(responseString);
|
||||
Log.d(TAG, "getDirections: " + result.getSteps().size());
|
||||
|
||||
for (DirectionsListener listener : listeners) {
|
||||
listener.onDirectionsAvailable(result);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
@@ -54,12 +66,10 @@ public enum ApiHandler {
|
||||
|
||||
t.start();
|
||||
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res.get();
|
||||
}
|
||||
|
||||
public void addListener(DirectionsListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.a1.nextlocation.network;
|
||||
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.json.DirectionsResult;
|
||||
|
||||
public interface DirectionsListener {
|
||||
void onDirectionsAvailable(DirectionsResult result);
|
||||
}
|
||||
Reference in New Issue
Block a user