Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.realdolmen.service;
- import be.realdolmen.logic.GpsLocation;
- import be.realdolmen.logic.Haversine;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- public class RoadsApiService {
- public JSONObject calcDistance(List<GpsLocation> gpsLocations){
- System.out.println("start setup");
- System.out.println("gpsLocations: " + gpsLocations);
- System.out.println("gpsLocations size :" + gpsLocations.size());
- List<GpsLocation> locationList = new ArrayList<GpsLocation>();
- List<Double> latitudes = new ArrayList<>();
- List<Double> longitudes = new ArrayList<>();
- double distance=0;
- int j = 0;
- int helpCounter =0;
- List<String> Requests = new ArrayList<>();
- int sizeCounter = gpsLocations.size();
- for (int i = 0; i < gpsLocations.size(); i++) {
- latitudes.add(gpsLocations.get(i).getLatitude());
- longitudes.add(gpsLocations.get(i).getLongitude());
- if (latitudes.size() >= 100){
- Requests.add(buildRequestStrings(latitudes,longitudes));
- latitudes.clear();
- longitudes.clear();
- }
- }
- Requests.add(buildRequestStrings(latitudes,longitudes));
- HttpURLConnection connection = null;
- //Make request with Requests
- for (int i= 0; i<Requests.size(); i++){
- try{
- URL url = new URL(Requests.get(i));
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- int responseCode = connection.getResponseCode();
- System.out.println("\nSending 'GET' request to URL : " + url);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- JSONObject jsonObject = new JSONObject(response.toString());
- JSONArray jsonArray = jsonObject.getJSONArray("snappedPoints");
- for(int z = 0; z<jsonArray.length();z++){
- JSONObject snappedPointJSONObject = jsonArray.getJSONObject(z);
- JSONObject locationJSONObject = snappedPointJSONObject.getJSONObject("location");
- locationList.add( new GpsLocation
- (
- Double.parseDouble(locationJSONObject.getString("latitude")),
- Double.parseDouble(locationJSONObject.getString("longitude"))
- )
- );
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- latitudes.clear();
- longitudes.clear();
- //haversine formula
- for (int i = 0;i<locationList.size();i++){
- latitudes.add(locationList.get(i).getLatitude());
- longitudes.add(locationList.get(i).getLongitude());
- }
- distance = 0;
- double distanceHelper =0;
- if (longitudes.size() == latitudes.size()){
- for (int i = 0; i<latitudes.size()-1;i++){
- distanceHelper = Haversine.distance(
- new Double(latitudes.get(i)),
- new Double(longitudes.get(i)),
- new Double(latitudes.get(i+1)),
- new Double(longitudes.get(i+1)));
- distance = distance + distanceHelper;
- distance = Math.round(distance *1000d)/1000d;
- }
- } else {
- System.out.println("incorrect lats/longs information");
- }
- System.out.println(distance+"Km");
- double outstandingAmount = 0.15+distance*0.02;
- System.out.println(outstandingAmount +" euro");
- return distanceJSONBuilder(distance,outstandingAmount);
- //return distance;
- }
- public JSONObject distanceJSONBuilder(double distance, double outstandingAmount){
- JSONObject responseJSON = new JSONObject();
- try {
- responseJSON.put("distance",distance);
- responseJSON.put("outstandingAmount",outstandingAmount);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- System.out.println(responseJSON);
- return responseJSON;
- }
- public String buildRequestStrings(List<Double> latitudes, List<Double> longitudes){
- String paramString="";
- String key="AIzaSyCNHq76IucfKOhspiM6sBlOpqWQCze8RvU";
- if (latitudes.size() == longitudes.size()){
- if (latitudes.size()> 100){
- return paramString ="More than 100 params in string!";
- } else {
- paramString="https://roads.googleapis.com/v1/snapToRoads?path=";
- for (int i = 0; i<latitudes.size(); i++){
- if (i == latitudes.size()-1){
- paramString+=latitudes.get(i) +","+longitudes.get(i);
- } else {
- paramString+=latitudes.get(i) +","+longitudes.get(i)+"|";
- }
- }
- paramString+="&interpolate=true&key="+key;
- }
- } else {
- return paramString ="amount of latitudes is not the same as the amount of longitudes";
- }
- return paramString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement