Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Button destination_next_button;
- static final String baseURL = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=";
- double distance = 0;
- double time = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- destination_next_button = (Button) findViewById(R.id.destination_next_button);
- destination_next_button.setOnClickListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- private void destination_next_buttonClick() {
- TextView tv = (TextView) findViewById(R.id.time_distance);
- tv.setText(data());
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.destination_next_button:
- destination_next_buttonClick();
- break;
- }
- }
- public String data(){
- TextView start = (TextView) findViewById(R.id.startinglocation);
- TextView end = (TextView) findViewById(R.id.endinglocation);
- String start1 = start.getText().toString();
- String end1 = end.getText().toString();
- StringBuilder URL = new StringBuilder(baseURL);
- URL.append(start1 + "&destinations=" + end1 + "&sensor=false");
- String fullUrl = URL.toString();
- JSONObject json = getJson(fullUrl);
- double distance = extractDistance(json);
- double duration = extractDuration(json);
- setDistance(distance);
- setTime(duration);
- return dataToString();
- }
- private double extractDistance(JSONObject json) {
- JSONArray rowsArray;
- double distanceInMiles = -1;
- try {
- // Getting Array of Distance Matrix Results
- rowsArray = json.getJSONArray("rows");
- JSONObject rowsObject = rowsArray.getJSONObject(0);//only one element in this array
- JSONArray elementsArray = rowsObject.getJSONArray("elements");
- JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
- JSONObject distanceObject = elementsObject.getJSONObject("distance");
- distanceInMiles = (distanceObject.getDouble("value"))/1609.344; //distance in meters converted to miles
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- return distanceInMiles;
- }
- private double extractDuration(JSONObject json) {
- JSONArray rowsArray = null;
- double distanceInMiles = -1;
- try {
- // Getting Array of Distance Matrix Results
- rowsArray = json.getJSONArray("rows");
- JSONObject rowsObject = rowsArray.getJSONObject(0);//only one element in this array
- JSONArray elementsArray = rowsObject.getJSONArray("elements");
- JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array
- JSONObject distanceObject = elementsObject.getJSONObject("duration");
- distanceInMiles = (distanceObject.getDouble("value"))/1609.344; //distance in meters converted to miles
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- return distanceInMiles;
- }
- public static JSONObject getJson(String url){
- InputStream is = null;
- String result = "";
- JSONObject jsonObject = null;
- // HTTP
- try {
- HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
- HttpPost httppost = new HttpPost(url);
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- } catch(Exception e) {
- return null;
- }
- // Read response to string
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line + "n");
- }
- is.close();
- result = sb.toString();
- } catch(Exception e) {
- return null;
- }
- // Convert string to object
- try {
- jsonObject = new JSONObject(result);
- } catch(JSONException e) {
- return null;
- }
- return jsonObject;
- }
- public void setTime(double t){
- time = t;
- }
- public void setDistance(double d){
- distance = d;
- }
- public String dataToString(){
- return "Total Distance: " + distance + ", Total Duration: " + time;
- }
- "rows" : [
- {
- "elements" : [
- {
- "distance" : {
- "text" : "1,743 km",
- "value" : 1742628
- },
- "duration" : {
- "text" : "15 hours 28 mins",
- "value" : 55705
- },
- "status" : "OK"
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement