Advertisement
rachmadi

DirectionJSONParser

Jun 22nd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package com.d4mdp.plesir.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import com.google.android.gms.maps.model.LatLng;
  12.  
  13. public class DirectionsJSONParser {
  14.    
  15.     /** Receives a JSONObject and returns a list of lists containing latitude and longitude */
  16.     public List<List<HashMap<String,String>>> parse(JSONObject jObject){
  17.        
  18.         List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>();
  19.         JSONArray jRoutes = null;
  20.         JSONArray jLegs = null;
  21.         JSONArray jSteps = null;   
  22.        
  23.         try {
  24.            
  25.             jRoutes = jObject.getJSONArray("routes");
  26.            
  27.             /** Traversing all routes */
  28.             for(int i=0;i<jRoutes.length();i++){       
  29.                 jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
  30.                 List path = new ArrayList<HashMap<String, String>>();
  31.                 ArrayList<String> direction=new ArrayList<String>();
  32.                 for(int a=0;a<jLegs.length();a++){
  33.                     JSONObject json_data=jLegs.getJSONObject(a);
  34.                     JSONObject json_data2=json_data.getJSONObject("distance");
  35.                     direction.add(json_data2.getString("text"));
  36.                     //System.out.println(json_data);
  37.                     //System.out.println(direction.get(a));
  38.                 }
  39.                 /** Traversing all legs */
  40.                 for(int j=0;j<jLegs.length();j++){
  41.                     jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
  42.                    
  43.                     /** Traversing all steps */
  44.                     for(int k=0;k<jSteps.length();k++){
  45.                         String polyline = "";
  46.                         polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
  47.                         List<LatLng> list = decodePoly(polyline);
  48.                        
  49.                         /** Traversing all points */
  50.                         for(int l=0;l<list.size();l++){
  51.                             HashMap<String, String> hm = new HashMap<String, String>();
  52.                             hm.put("lat", Double.toString(((LatLng)list.get(l)).latitude) );
  53.                             hm.put("lng", Double.toString(((LatLng)list.get(l)).longitude) );
  54.                             path.add(hm);                      
  55.                         }                              
  56.                     }
  57.                     routes.add(path);
  58.                 }
  59.             }
  60.            
  61.         } catch (JSONException e) {        
  62.             e.printStackTrace();
  63.         }catch (Exception e){          
  64.         }
  65.        
  66.        
  67.         return routes;
  68.     }  
  69.    
  70.    
  71.     /**
  72.      * Method to decode polyline points
  73.      * Courtesy : jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
  74.      * */
  75.     private List<LatLng> decodePoly(String encoded) {
  76.  
  77.         List<LatLng> poly = new ArrayList<LatLng>();
  78.         int index = 0, len = encoded.length();
  79.         int lat = 0, lng = 0;
  80.  
  81.         while (index < len) {
  82.             int b, shift = 0, result = 0;
  83.             do {
  84.                 b = encoded.charAt(index++) - 63;
  85.                 result |= (b & 0x1f) << shift;
  86.                 shift += 5;
  87.             } while (b >= 0x20);
  88.             int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  89.             lat += dlat;
  90.  
  91.             shift = 0;
  92.             result = 0;
  93.             do {
  94.                 b = encoded.charAt(index++) - 63;
  95.                 result |= (b & 0x1f) << shift;
  96.                 shift += 5;
  97.             } while (b >= 0x20);
  98.             int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  99.             lng += dlng;
  100.  
  101.             LatLng p = new LatLng((((double) lat / 1E5)),
  102.                     (((double) lng / 1E5)));
  103.             poly.add(p);
  104.         }
  105.  
  106.         return poly;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement