代码之家  ›  专栏  ›  技术社区  ›  Maksym Gontar

在Android中通过经度和纬度获取高度

  •  36
  • Maksym Gontar  · 技术社区  · 16 年前

    在Android平台上,有没有一种快速而有效的方法可以通过经度和纬度来获得海拔高度(海拔)?

    7 回复  |  直到 9 年前
        1
  •  39
  •   Maksym Gontar    16 年前

    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;
                }
            });
        }
    
        2
  •  23
  •   Community Mohan Dere    9 年前

    您也可以使用GoogleElevationAPI。其在线文档位于: https://developers.google.com/maps/documentation/elevation/

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

    使用限制:使用谷歌 地理编码API需要查询 2500个地理位置请求的限制 每天。(谷歌地图API用户 Premier最多可执行100000次 每天请求。)此限制为 为防止滥用和/或 重新调整地理编码API的用途,以及 此限制可能在 未来,恕不另行通知。此外, 我们将请求率限制为 防止滥用服务。如果你 超过24小时限制或其他 滥用服务,地理编码API 可能暂时停止为你工作。 如果你继续超过这个限度, 您可以访问geocoding api 被封锁。注:地理编码API 只能与 谷歌地图;地理编码结果 在地图上显示它们是 禁止。有关 允许使用,请参阅maps api 服务许可条款限制。

    改变 Max Gontar's 上面的google api代码给出了以下内容,返回的高度以英尺为单位:

    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;
        }
    
        3
  •  3
  •   Always Lucky    9 年前

    首先区分海拔和海拔是很重要的。

    海拔是从一个点到当地表面的距离,不管是陆地还是水。该测量主要用于航空。

    高度可通过 Location.getAltitude() 功能。

    海拔是指从当地表面到海平面的距离;经常被使用,经常被错误地称为海拔。

    尽管如此,美国地质勘探局为美国提供了 newer HTTP POST and GET queries 它可以返回XML或JSON值,以英尺或米为单位表示高程。对于全球海拔,您可以使用 Google Elevation API .

        4
  •  1
  •   user1703603    14 年前

    如果你使用的安卓设备有GPS接收器,那么有一种方法 GETALTIUTUDE() 用这个可以得到海拔高度。

        5
  •  0
  •   Petter Friberg Onceler    10 年前

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

    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;
    }
    
        6
  •  0
  •   Gaploid    10 年前

    试试我做的这个: https://algorithmia.com/algorithms/Gaploid/Elevation

    下面是Java的例子:

    import com.algorithmia.*;
    import com.algorithmia.algo.*;
    
    String input = "{\"lat\": \"50.2111\", \"lon\": \"18.1233\"}";
    AlgorithmiaClient client = Algorithmia.client("YOUR_API_KEY");
    Algorithm algo = client.algo("algo://Gaploid/Elevation/0.3.0");
    AlgoResponse result = algo.pipeJson(input);
    System.out.println(result.asJson());
    
        7
  •  -2
  •   M66B    11 年前

    使用GoogleElevationAPI的想法是好的,但是使用字符串函数解析XML却不是好的。另外,现在不推荐使用httpclient,因为它使用不安全的连接。

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