Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. package be.realdolmen.service;
  2.  
  3. import be.realdolmen.logic.GpsLocation;
  4. import be.realdolmen.logic.Haversine;
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. public class RoadsApiService {
  17.     public JSONObject calcDistance(List<GpsLocation> gpsLocations){
  18.  
  19.         System.out.println("start setup");
  20.         System.out.println("gpsLocations: " + gpsLocations);
  21.         System.out.println("gpsLocations size :" + gpsLocations.size());
  22.         List<GpsLocation> locationList = new ArrayList<GpsLocation>();
  23.         List<Double> latitudes = new ArrayList<>();
  24.         List<Double> longitudes = new ArrayList<>();
  25.         double distance=0;
  26.         int j = 0;
  27.         int helpCounter =0;
  28.         List<String> Requests = new ArrayList<>();
  29.         int sizeCounter = gpsLocations.size();
  30.  
  31.  
  32.  
  33.             for (int i = 0; i < gpsLocations.size(); i++) {
  34.                 latitudes.add(gpsLocations.get(i).getLatitude());
  35.                 longitudes.add(gpsLocations.get(i).getLongitude());
  36.                 if (latitudes.size() >= 100){
  37.                     Requests.add(buildRequestStrings(latitudes,longitudes));
  38.                     latitudes.clear();
  39.                     longitudes.clear();
  40.                 }
  41.             }
  42.             Requests.add(buildRequestStrings(latitudes,longitudes));
  43.  
  44.  
  45.             HttpURLConnection connection = null;
  46.             //Make request with Requests
  47.         for (int i= 0; i<Requests.size(); i++){
  48.             try{
  49.                 URL url = new URL(Requests.get(i));
  50.                 connection = (HttpURLConnection) url.openConnection();
  51.  
  52.                connection.setRequestMethod("GET");
  53.                int responseCode = connection.getResponseCode();
  54.                 System.out.println("\nSending 'GET' request to URL : " + url);
  55.                 System.out.println("Response Code : " + responseCode);
  56.  
  57.                 BufferedReader in = new BufferedReader(
  58.                         new InputStreamReader(connection.getInputStream()));
  59.                 String inputLine;
  60.                 StringBuffer response = new StringBuffer();
  61.  
  62.                 while ((inputLine = in.readLine()) != null) {
  63.                     response.append(inputLine);
  64.  
  65.                 }
  66.                 JSONObject jsonObject = new JSONObject(response.toString());
  67.                 JSONArray jsonArray = jsonObject.getJSONArray("snappedPoints");
  68.  
  69.  
  70.                 for(int z = 0; z<jsonArray.length();z++){
  71.                     JSONObject snappedPointJSONObject = jsonArray.getJSONObject(z);
  72.                     JSONObject locationJSONObject = snappedPointJSONObject.getJSONObject("location");
  73.                     locationList.add( new GpsLocation
  74.                             (
  75.                                     Double.parseDouble(locationJSONObject.getString("latitude")),
  76.                                     Double.parseDouble(locationJSONObject.getString("longitude"))
  77.                             )
  78.                     );
  79.                 }
  80.                 in.close();
  81.             } catch (Exception e) {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.  
  86.         latitudes.clear();
  87.         longitudes.clear();
  88.         //haversine formula
  89.         for (int i = 0;i<locationList.size();i++){
  90.             latitudes.add(locationList.get(i).getLatitude());
  91.             longitudes.add(locationList.get(i).getLongitude());
  92.         }
  93.         distance = 0;
  94.         double distanceHelper =0;
  95.         if (longitudes.size() == latitudes.size()){
  96.             for (int i = 0; i<latitudes.size()-1;i++){
  97.                 distanceHelper = Haversine.distance(
  98.                         new Double(latitudes.get(i)),
  99.                         new Double(longitudes.get(i)),
  100.                         new Double(latitudes.get(i+1)),
  101.                         new Double(longitudes.get(i+1)));
  102.                 distance = distance + distanceHelper;
  103.                 distance = Math.round(distance *1000d)/1000d;
  104.             }
  105.         } else {
  106.             System.out.println("incorrect lats/longs information");
  107.         }
  108.         System.out.println(distance+"Km");
  109.         double outstandingAmount = 0.15+distance*0.02;
  110.         System.out.println(outstandingAmount +" euro");
  111.  
  112.  
  113.         return distanceJSONBuilder(distance,outstandingAmount);
  114.         //return distance;
  115.  
  116.     }
  117.  
  118.     public JSONObject distanceJSONBuilder(double distance, double outstandingAmount){
  119.         JSONObject responseJSON = new JSONObject();
  120.         try {
  121.             responseJSON.put("distance",distance);
  122.             responseJSON.put("outstandingAmount",outstandingAmount);
  123.         } catch (JSONException e) {
  124.             e.printStackTrace();
  125.         }
  126.  
  127.         System.out.println(responseJSON);
  128.         return responseJSON;
  129.     }
  130.  
  131.     public String buildRequestStrings(List<Double> latitudes, List<Double> longitudes){
  132.         String paramString="";
  133.         String key="AIzaSyCNHq76IucfKOhspiM6sBlOpqWQCze8RvU";
  134.         if (latitudes.size() == longitudes.size()){
  135.             if (latitudes.size()> 100){
  136.                 return paramString ="More than 100 params in string!";
  137.             } else {
  138.                 paramString="https://roads.googleapis.com/v1/snapToRoads?path=";
  139.                 for (int i = 0; i<latitudes.size(); i++){
  140.                     if (i == latitudes.size()-1){
  141.                         paramString+=latitudes.get(i) +","+longitudes.get(i);
  142.                     } else {
  143.                         paramString+=latitudes.get(i) +","+longitudes.get(i)+"|";
  144.                     }
  145.                 }
  146.                 paramString+="&interpolate=true&key="+key;
  147.             }
  148.         } else {
  149.             return paramString ="amount of latitudes is not the same as the amount of longitudes";
  150.         }
  151.         return paramString;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement