1. Are you ready for the Galaxy S20? Here is everything we know so far!

GPS Location Distance

Discussion in 'Android Development' started by Ravi Pratap Singh, Jul 17, 2017.

  1. Ravi Pratap Singh

    Thread Starter

    How we find exact distance b/w two point on gmap in android ?
    I am using this

    Code (Text):
    1.  
    2. public String getDistance(final double lat1, final double lon1, final double lat2, final double lon2){
    3.     String parsedDistance;
    4.     String response;
    5.     Thread thread=new Thread(new Runnable() {
    6.         [USER=1021285]@override[/USER]
    7.         public void run() {
    8.             try {
    9.  
    10.                 URL url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric&mode=driving");
    11.                 final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    12.                 conn.setRequestMethod("POST");
    13.                 InputStream in = new BufferedInputStream(conn.getInputStream());
    14.               String  response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
    15.  
    16.                 JSONObject jsonObject = new JSONObject(response);
    17.                 JSONArray array = jsonObject.getJSONArray("routes");
    18.                 JSONObject routes = array.getJSONObject(0);
    19.                 JSONArray legs = routes.getJSONArray("legs");
    20.                 JSONObject steps = legs.getJSONObject(0);
    21.                 JSONObject distance = steps.getJSONObject("distance");
    22.               String  parsedDistance=distance.getString("text");
    23.  
    24.             } catch (ProtocolException e) {
    25.                 e.printStackTrace();
    26.             } catch (MalformedURLException e) {
    27.                 e.printStackTrace();
    28.             } catch (IOException e) {
    29.                 e.printStackTrace();
    30.             } catch (JSONException e) {
    31.                 e.printStackTrace();
    32.             }
    33.         }
    34.     });
    35.     thread.start();
    36.     try {
    37.         thread.join();
    38.     } catch (InterruptedException e) {
    39.         e.printStackTrace();
    40.     }
    41.     return parsedDistance;
    42. }
    43.  
    but error is comming in this line
    String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
     


    #1 Ravi Pratap Singh, Jul 17, 2017
    Last edited by a moderator: Jul 17, 2017

    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    What's the error? If the application crashed, please include the stack trace from the Logcat view.
     
  3. Ravi Pratap Singh

    Thread Starter

    I solved the location search problem but now i want to generate the SOS alert on G Map.how i can do it
     
  4. Phalon4

    Phalon4 Android Enthusiast

    Be careful dabbling in those type of programs you're going to raise some unwanted suspicions.
     
Loading...
Similar Threads - GPS Location Distance
  1. watchintv
    Replies:
    9
    Views:
    360
  2. watchintv
    Replies:
    2
    Views:
    310
  3. rshimizu12
    Replies:
    3
    Views:
    425
  4. Zoran Nikolovski
    Replies:
    2
    Views:
    1,124
  5. raczosak
    Replies:
    2
    Views:
    1,273
  6. App Update
    Replies:
    1
    Views:
    519
  7. GPSAngularCamera
    Replies:
    1
    Views:
    804
  8. App Update
    Replies:
    0
    Views:
    869
  9. Raza Shaikh
    Replies:
    1
    Views:
    3,144
  10. MikeSD
    Replies:
    5
    Views:
    2,120

Share This Page

Loading...