1

I am trying to get altitude but it is not getting any rather garbage value. I have followed this link: Get altitude by longitude and latitude in Android.

protected String doInBackground(String... params) {
    try {
        URL url=new URL("http://gisdata.usgs.gov/"
                + "xmlwebservices2/elevation_service.asmx/"
                + "getElevation?X_Value=" + params[1]
                + "&Y_Value=" + params[0]
                + "&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true");

        HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        OutputStream outputStream=httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

        bufferedWriter.flush();
        bufferedWriter.close();

        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String line = "";

        double res = Double.NaN;

        while ((line = bufferedReader.readLine()) != null) {

            int r = -1;
            StringBuffer respStr = new StringBuffer();
            while ((r = inputStream.read()) != -1)
                respStr.append((char) r);
            String tagOpen = "<double>";
            String tagClose = "</double>";
            if (respStr.indexOf(tagOpen) != -1) {
                int start = respStr.indexOf(tagOpen) + tagOpen.length();
                int end = respStr.indexOf(tagClose);
                String value = respStr.substring(start, end);
                res = Double.parseDouble(value);
            }
        }

        inputStream.close();
        httpURLConnection.disconnect();

        Log.v("Altitude D",String.valueOf(res));

        return String.valueOf(res);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

and the way i have called it from onLocationChanged.

public void onLocationChanged(Location location) {
    if (location == null) {           
    } 
    else {
        Altitude altitude=new Altitude();
        altitude.execute(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
    }
}
Community
  • 1
  • 1
AAA
  • 485
  • 6
  • 23
  • see this link: http://stackoverflow.com/questions/1995998/android-get-altitude-by-longitude-and-latitude – Dante Jun 05 '16 at 05:57
  • i have following that link and above code is the modification of that code but there are some error that the url is not able to show page – AAA Jun 05 '16 at 06:03

1 Answers1

0

You need to call the runnable, something like this.

public void onLocationChanged(Location location) {
    if (location == null) {           
    } 
    else {
        new Thread(new Runnable() {
            public void run() {
                final double alt = getAltitude(lon, lat);
            }
        }).run();
    }

You are attempting to get the value of it from a string, when you have already parsed it to double.

        double res = Double.NaN;

        while ((line = bufferedReader.readLine()) != null) {

            int r = -1;
            StringBuffer respStr = new StringBuffer();
            while ((r = inputStream.read()) != -1)
                respStr.append((char) r);
            String tagOpen = "<double>";
            String tagClose = "</double>";
            if (respStr.indexOf(tagOpen) != -1) {
                int start = respStr.indexOf(tagOpen) + tagOpen.length();
                int end = respStr.indexOf(tagClose);
                String value = respStr.substring(start, end);
                // parsing to  double here.                           
                res = Double.parseDouble(value);
            }
        }

        inputStream.close();
        httpURLConnection.disconnect();

        Log.v("Altitude D",String.valueOf(res));

       // return String.valueOf(res); get rid of this line
       return res;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // or return res down here.
    return res;
}
  • thank you ma'am,but onPostExecute of AsyncTask showing error trying with return just Double. that is the problem – AAA Jun 05 '16 at 06:22
  • ok ma'am. but it will be very helpful to me if you could provide it fast. BTW thanks once again :) – AAA Jun 05 '16 at 06:34
  • i am calling the class by this Altitude altitude=new Altitude(); altitude.execute(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())); from onLocationChanged – AAA Jun 05 '16 at 06:36