在Android中获取经度和纬度的海拔高度

时间:2010-01-03 19:30:26

标签: android geolocation gis android-mapview

在Android平台上有经济和自由度快速有效的方法来获取海拔(海拔)吗?

7 个答案:

答案 0 :(得分:39)

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

我的方法是使用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;
}

使用示例(在HelloMapView类中):

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

答案 1 :(得分:23)

您还可以使用Google Elevation API。它的在线文档位于: https://developers.google.com/maps/documentation/elevation/

请注意以上API页面中的以下内容:

  

使用限制:使用Google   地理编码API受查询限制   限制2,500个地理位置请求   每天。 (Google Maps API的用户   总理最高可达100,000   请求每天。)此限制是   强制执行以防止滥用和/或   重新利用Geocoding API,和   这个限制可能会改变   未来不经通知。另外,   我们强制执行请求率限制   防止滥用服务。如果你   超过24小时限制或其他   滥用服务,Geocoding API   可能会暂时停止为你工作。   如果您继续超过此限制,   您可以访问Geocoding API   被封锁注意:地理编码API   只能与a一起使用   谷歌地图;地理编码结果没有   在地图上显示它们是   禁止。有关的完整详细信息   允许使用,请参阅Maps API   服务条款许可限制。

为Google API更改上面的Max Gontar's代码,提供以下内容,返回的高程以英尺为单位:

private double getElevationFromGoogleMaps(double longitude, double latitude) {
        double result = Double.NaN;
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        String url = "http://maps.googleapis.com/maps/api/elevation/"
                + "xml?locations=" + String.valueOf(latitude)
                + "," + String.valueOf(longitude)
                + "&sensor=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 = "<elevation>";
                String tagClose = "</elevation>";
                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)(Double.parseDouble(value)*3.2808399); // convert from meters to feet
                }
                instream.close();
            }
        } catch (ClientProtocolException e) {} 
        catch (IOException e) {}

        return result;
    }

答案 2 :(得分:3)

首先要区分海拔高度,这一点非常重要。

海拔高度是从一个点到局部表面的距离;无论是土地还是水。该测量主要用于航空。

可以使用Location.getAltitude()函数获取海拔高度。

高程是从地表到海平面的距离;更经常使用,经常被错误地称为高度。

据说,对于美国,USGS提供了一个newer HTTP POST and GET queries,可以返回任意英尺或米的高程的XML或JSON值。对于全球海拔高度,您可以使用Google Elevation API

答案 3 :(得分:1)

如果您正在使用具有GPS接收器的Android设备,那么有一种方法 getAltitude(),使用它可以通过高程获得高度。

答案 4 :(得分:0)

谷歌地图有高度,你需要的是这段代码

altitude="";
var init = function() {
        var elevator = new google.maps.ElevationService;
        map.on('mousemove', function(event) {
            getLocationElevation(event.latlng, elevator);
            document.getElementsByClassName("altitudeClass")[0].innerHTML = "Altitude: "+ getAltitude();
            //console.debug(getAltitude());
        });
}

var getLocationElevation = function (location, elevator){
  // Initiate the location request
  elevator.getElevationForLocations({
    'locations': [location]
  }, function(results, status) {
    if (status === google.maps.ElevationStatus.OK) {
      // Retrieve the first result
      if (results[0]) {
        // Open the infowindow indicating the elevation at the clicked position.
        setAltitude(parseFloat(results[0].elevation).toFixed(2));
      } else {
        setAltitude('No results found');
      }
    } else {
      setAltitude('Elevation service failed due to: ' + status);
    }
  });
}
function setAltitude(a){
    altitude = a;
}
function getAltitude(){
    return altitude;
}

答案 5 :(得分:0)

尝试我建立的这个:https://algorithmia.com/algorithms/Gaploid/Elevation

这是Java的例子:

np.ravel_multi_index

答案 6 :(得分:-2)

使用Google Elevation API的想法很好,但使用字符串函数解析XML却不行。此外,现在不推荐使用HttpClient,因为使用了不安全的连接。

请点击此处获取更好的解决方案: https://github.com/M66B/BackPackTrackII/blob/master/app/src/main/java/eu/faircode/backpacktrack2/GoogleElevationApi.java