如何从Google地图获取高度

时间:2016-06-05 05:49:16

标签: android google-maps

我正试图获得高度,但它没有得到任何相当的垃圾价值。我已关注此链接: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;
}

以及我从onLocationChanged调用它的方式。

public void onLocationChanged(Location location) {
    if (location == null) {           
    } 
    else {
        Altitude altitude=new Altitude();
        altitude.execute(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
    }
}

1 个答案:

答案 0 :(得分:0)

你需要调用runnable,就像这样。

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

当你已经将它解析为加倍时,你试图从字符串中获取它的值。

        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;
}