在Android中根据经度和纬度获取高度 [英] Get altitude by longitude and latitude in Android

查看:157
本文介绍了在Android中根据经度和纬度获取高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海拔应用程序屏幕http://img509.imageshack.us/img509/4848/elevationc.jpg



我的方法是使用 USGS海拔查询网络服务

  private double getAltitude(Double Longitude,Double latitude){
double结果= Double.NaN;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String url =http://gisdata.usgs.gov/
+xmlwebservices2 / elevation_service.asmx /
+getElevation?X_Value =+ String.valueOf(longitude)
+& Y_Value =+ String.valueOf(latitude)
+& Elevation_Units = METERS& Source_Layer = -1& Elevation_Only = true;
HttpGet httpGet = new HttpGet(url);
尝试{
HttpResponse response = httpClient.execute(httpGet,localContext);
HttpEntity entity = response.getEntity();
if(entity!= null){
InputStream instream = entity.getContent();
int r = -1;
StringBuffer respStr = new StringBuffer(); ((r = instream.read())!= -1)

respStr.append((char)r);
字符串tagOpen =< double>;
字符串tagClose =< / double>;
if(respStr.indexOf(tagOpen)!= -1){
int start = respStr.indexOf(tagOpen)+ tagOpen.length();
int end = respStr.indexOf(tagClose);
字符串值= respStr.substring(start,end);
result = Double.parseDouble(value);
}
instream.close();
}
} catch(ClientProtocolException e){}
catch(IOException e){}
return result;
}

使用示例(在

 

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout =(LinearLayout)findViewById(R.id.zoomview);
mapView =(MapView)findViewById(R.id.mapview);
mZoom =(ZoomControls)mapView.getZoomControls();
linearLayout.addView(mZoom);
mapView.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){$ b $ if(event.getAction()== 1){
final GeoPoint p = mapView.getProjection()。fromPixels(
(int)event.getX(),(int)event.getY());
final StringBuilder msg = new StringBuilder();
new Thread(new Runnable(){
public void run(){
final double lon = p.getLongitudeE6()/ 1E6;
final double lat = p.getLatitudeE6()/ 1E6;
final double alt = getAltitude(lon,lat);
msg.append(Lon:);
msg.append(lon);
msg.append(Lat :);
msg.append(lat);
msg.append(Alt :);
msg.append(alt);
}
})。run();
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT)
.show();
}
返回false;
}
});
}


Is there a quick and efficient way to get altitude (elevation) by longitude and latitude on the Android platform?

elevation app screen http://img509.imageshack.us/img509/4848/elevationc.jpg

My approach is to use USGS Elevation Query Web Service:

private double getAltitude(Double longitude, Double latitude) {
    double result = Double.NaN;
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    String url = "http://gisdata.usgs.gov/"
            + "xmlwebservices2/elevation_service.asmx/"
            + "getElevation?X_Value=" + String.valueOf(longitude)
            + "&Y_Value=" + String.valueOf(latitude)
            + "&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true";
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = httpClient.execute(httpGet, localContext);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            int r = -1;
            StringBuffer respStr = new StringBuffer();
            while ((r = instream.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);
                result = Double.parseDouble(value);
            }
            instream.close();
        }
    } catch (ClientProtocolException e) {} 
    catch (IOException e) {}
    return result;
}

And example of use (right in HelloMapView class):

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        linearLayout = (LinearLayout) findViewById(R.id.zoomview);
        mapView = (MapView) findViewById(R.id.mapview);
        mZoom = (ZoomControls) mapView.getZoomControls();
        linearLayout.addView(mZoom);
        mapView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == 1) {
                    final GeoPoint p = mapView.getProjection().fromPixels(
                            (int) event.getX(), (int) event.getY());
                    final StringBuilder msg = new StringBuilder();
                    new Thread(new Runnable() {
                        public void run() {
                            final double lon = p.getLongitudeE6() / 1E6;
                            final double lat = p.getLatitudeE6() / 1E6;
                            final double alt = getAltitude(lon, lat);
                            msg.append("Lon: ");
                            msg.append(lon);
                            msg.append(" Lat: ");
                            msg.append(lat);
                            msg.append(" Alt: ");
                            msg.append(alt);
                        }
                    }).run();
                    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT)
                            .show();
                }
                return false;
            }
        });
    }

这篇关于在Android中根据经度和纬度获取高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆