Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. Button destination_next_button;
  2. static final String baseURL = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=";
  3. double distance = 0;
  4. double time = 0;
  5.  
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. destination_next_button = (Button) findViewById(R.id.destination_next_button);
  12. destination_next_button.setOnClickListener(this);
  13.  
  14. }
  15.  
  16. @Override
  17. public boolean onCreateOptionsMenu(Menu menu) {
  18. getMenuInflater().inflate(R.menu.main, menu);
  19. return true;
  20. }
  21.  
  22. private void destination_next_buttonClick() {
  23. TextView tv = (TextView) findViewById(R.id.time_distance);
  24. tv.setText(data());
  25. }
  26.  
  27. @Override
  28. public void onClick(View v) {
  29. switch (v.getId()) {
  30. case R.id.destination_next_button:
  31. destination_next_buttonClick();
  32. break;
  33. }
  34. }
  35.  
  36. public String data(){
  37. TextView start = (TextView) findViewById(R.id.startinglocation);
  38. TextView end = (TextView) findViewById(R.id.endinglocation);
  39.  
  40. String start1 = start.getText().toString();
  41. String end1 = end.getText().toString();
  42.  
  43. StringBuilder URL = new StringBuilder(baseURL);
  44. URL.append(start1 + "&destinations=" + end1 + "&sensor=false");
  45. String fullUrl = URL.toString();
  46.  
  47. JSONObject json = getJson(fullUrl);
  48.  
  49. double distance = extractDistance(json);
  50. double duration = extractDuration(json);
  51.  
  52. setDistance(distance);
  53. setTime(duration);
  54.  
  55. return dataToString();
  56. }
  57.  
  58. private double extractDistance(JSONObject json) {
  59. JSONArray rowsArray;
  60. double distanceInMiles = -1;
  61. try {
  62. // Getting Array of Distance Matrix Results
  63. rowsArray = json.getJSONArray("rows");
  64. JSONObject rowsObject = rowsArray.getJSONObject(0);//only one element in this array
  65. JSONArray elementsArray = rowsObject.getJSONArray("elements");
  66. JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
  67. JSONObject distanceObject = elementsObject.getJSONObject("distance");
  68. distanceInMiles = (distanceObject.getDouble("value"))/1609.344; //distance in meters converted to miles
  69. }
  70. catch (JSONException e) {
  71. e.printStackTrace();
  72. }
  73. return distanceInMiles;
  74. }
  75.  
  76. private double extractDuration(JSONObject json) {
  77. JSONArray rowsArray = null;
  78. double distanceInMiles = -1;
  79. try {
  80. // Getting Array of Distance Matrix Results
  81. rowsArray = json.getJSONArray("rows");
  82. JSONObject rowsObject = rowsArray.getJSONObject(0);//only one element in this array
  83. JSONArray elementsArray = rowsObject.getJSONArray("elements");
  84. JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
  85. JSONObject distanceObject = elementsObject.getJSONObject("duration");
  86. distanceInMiles = (distanceObject.getDouble("value"))/1609.344; //distance in meters converted to miles
  87. }
  88. catch (JSONException e) {
  89. e.printStackTrace();
  90. }
  91. return distanceInMiles;
  92. }
  93.  
  94. public static JSONObject getJson(String url){
  95.  
  96. InputStream is = null;
  97. String result = "";
  98. JSONObject jsonObject = null;
  99.  
  100. // HTTP
  101. try {
  102. HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
  103. HttpPost httppost = new HttpPost(url);
  104. HttpResponse response = httpclient.execute(httppost);
  105. HttpEntity entity = response.getEntity();
  106. is = entity.getContent();
  107. } catch(Exception e) {
  108. return null;
  109. }
  110.  
  111. // Read response to string
  112. try {
  113. BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
  114. StringBuilder sb = new StringBuilder();
  115. String line = null;
  116. while ((line = reader.readLine()) != null) {
  117. sb.append(line + "n");
  118. }
  119. is.close();
  120. result = sb.toString();
  121. } catch(Exception e) {
  122. return null;
  123. }
  124.  
  125. // Convert string to object
  126. try {
  127. jsonObject = new JSONObject(result);
  128. } catch(JSONException e) {
  129. return null;
  130. }
  131.  
  132. return jsonObject;
  133.  
  134. }
  135.  
  136. public void setTime(double t){
  137. time = t;
  138. }
  139.  
  140. public void setDistance(double d){
  141. distance = d;
  142. }
  143.  
  144. public String dataToString(){
  145.  
  146. return "Total Distance: " + distance + ", Total Duration: " + time;
  147. }
  148.  
  149. "rows" : [
  150.  
  151. {
  152.  
  153. "elements" : [
  154.  
  155. {
  156.  
  157. "distance" : {
  158.  
  159. "text" : "1,743 km",
  160.  
  161. "value" : 1742628
  162.  
  163. },
  164.  
  165. "duration" : {
  166.  
  167. "text" : "15 hours 28 mins",
  168.  
  169. "value" : 55705
  170.  
  171. },
  172.  
  173. "status" : "OK"
  174.  
  175. }
  176.  
  177. ]
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement