Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. // rota inicial,e rota da unit
  2. public void getEndereco() throws UnsupportedEncodingException {
  3. String o = String.valueOf(latitude +","+ longitude);
  4. String d = "Universidade Tiradentes - Avenida Murilo Dantas - Farolândia, Aracaju - SE";
  5. String origin = URLEncoder.encode(o, "UTF-8");
  6. String destination = URLEncoder.encode(d, "UTF-8");
  7. }
  8.  
  9.  
  10. // WEB CONNECTION
  11. public void getRoute(final String origin, final String destination){
  12.  
  13. new Thread(){
  14. public void run(){
  15. String url= "http://maps.googleapis.com/maps/api/directions/json?origin="
  16. + origin+"&destination="
  17. + destination+"&sensor=false";
  18.  
  19. HttpResponse response;
  20. HttpGet request;
  21. AndroidHttpClient client = AndroidHttpClient.newInstance("route");
  22.  
  23. request = new HttpGet(url);
  24. try {
  25. response = client.execute(request);
  26. final String answer = EntityUtils.toString(response.getEntity());
  27.  
  28. runOnUiThread(new Runnable(){
  29. public void run(){
  30. try {
  31. //Log.i("Script", answer);
  32. list = buildJSONRoute(answer);
  33. drawRoute();
  34. }
  35. catch(JSONException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. });
  40.  
  41. }
  42. catch(IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }.start();
  47. }
  48.  
  49.  
  50.  
  51. //JSON
  52. public List<LatLng> buildJSONRoute(String json) throws JSONException {
  53. JSONObject result = new JSONObject(json);
  54. JSONArray routes = result.getJSONArray("routes");
  55.  
  56. long distanceForSegment = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("values");
  57. JSONArray steps = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
  58. List<LatLng> lines = new ArrayList<LatLng>();
  59.  
  60. for (int i = 0; i < steps.length(); i++) {
  61. Log.i("Script", "STEP: LAT: " + steps.getJSONObject(i).getJSONObject("start_location").getDouble("lat") + " | LNG: " + steps.getJSONObject(i).getJSONObject("start_location").getDouble("lng"));
  62.  
  63.  
  64. String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points");
  65.  
  66. for (LatLng p : decodePolyline(polyline)) {
  67. lines.add(p);
  68. }
  69.  
  70. Log.i("Script", "STEP: LAT: " + steps.getJSONObject(i).getJSONObject("end_location").getDouble("lat") + " | LNG: " + steps.getJSONObject(i).getJSONObject("end_location").getDouble("lng"));
  71. }
  72.  
  73. return lines;
  74. }
  75.  
  76.  
  77. // DECODE POLYLINE
  78. private List<LatLng> decodePolyline(String encoded) {
  79.  
  80. List<LatLng> listPoints = new ArrayList<LatLng>();
  81. int index = 0, len = encoded.length();
  82. int lat = 0, lng = 0;
  83.  
  84. while (index < len) {
  85. int b, shift = 0, result = 0;
  86. do {
  87. b = encoded.charAt(index++) - 63;
  88. result |= (b & 0x1f) << shift;
  89. shift += 5;
  90. } while (b >= 0x20);
  91. int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  92. lat += dlat;
  93.  
  94. shift = 0;
  95. result = 0;
  96. do {
  97. b = encoded.charAt(index++) - 63;
  98. result |= (b & 0x1f) << shift;
  99. shift += 5;
  100. } while (b >= 0x20);
  101. int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  102. lng += dlng;
  103.  
  104. LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
  105. Log.i("Script", "POL: LAT: " + p.latitude + " | LNG: " + p.longitude);
  106. listPoints.add(p);
  107. }
  108. return listPoints;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement