1. Check out our companion app, Forums for Android! Download from Google Play

Apps GPS Location Distance

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

  1. Ravi Pratap Singh

    Ravi Pratap Singh New Member
    Thread Starter
    5

    Jul 17, 2017
    2
    0
    5
    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");
     

    Advertisement

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

    LV426 Illegitimi non carborundum
    Moderator
    1,238

    Oct 16, 2015
    4,279
    5,525
    1,238
    Male
    Software developer
    South West of England
    What's the error? If the application crashed, please include the stack trace from the Logcat view.
     
  3. Ravi Pratap Singh

    Ravi Pratap Singh New Member
    Thread Starter
    5

    Jul 17, 2017
    2
    0
    5
    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 Well-Known Member
    108

    Apr 18, 2016
    444
    157
    108
    Be careful dabbling in those type of programs you're going to raise some unwanted suspicions.
     

Share This Page

Loading...