【问题标题】:Getting altitude tapping on a place of the map在地图上的某个地方获取高度
【发布时间】:2012-10-18 23:39:32
【问题描述】:

我使用以下代码获取坐标和高度,只需单击地图上的某个位置...例如,如果我单击纽约街道,应用程序会返回给我纽约坐标

public void onLocationChanged(Location location) {
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
    double alt = (double) (location.getAltitude());
}

我使用这个方法来检索一个地方的触摸位置数据

@Override
public boolean onTap(GeoPoint p, MapView map) {     

    List<Overlay> overlays = map.getOverlays();

    Message message = new Message();

    Bundle data = new Bundle();

    data.putInt("latitude", p.getLatitudeE6());

    data.putInt("longitude", p.getLongitudeE6());

    message.setData(data);

    handler.sendMessage(message);       

    return super.onTap(p, map);
}   

不幸的是,GeoPoint 对象似乎没有像 p.getAltitudeE6() 这样的方法来模拟经度和纬度。

因此,点击地图获取纬度和经度可以正常工作,但高度在每个点击的地方都会返回 0。

有没有办法解决这个问题?

【问题讨论】:

    标签: android android-maps altitude geopositioning


    【解决方案1】:

    地图数据不包括海拔信息。您可以使用Google Elevation API 获取您拥有的纬度和经度并获得海拔高度。这是一个在线查找,因此不一定非常敏感。

    添加代码示例。

    public double getAltitudeFromNet(double def, Location loc) {
    Log.v(TAG, "Looking up net altitude");
    double result = -100000.0;
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 5000);
    
    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();
    String url = "http://maps.googleapis.com/maps/api/elevation/" + "xml?locations="
        + String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "&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));
    
        }
        instream.close();
      }
    } catch (ClientProtocolException e) {
      Log.w(TAG, "Looking up net altitude ClientProtocolException", e);
    } catch (IOException e) {
      Log.w(TAG, "Looking up net altitude IOException", e);
    }
    
    Log.i(TAG, "got net altitude " + (int) result);
    if (result > -1000) {
      return result;
    } else {
      return def;
    }
    }
    

    【讨论】:

    • 感谢您的回答。你能给我一个关于在Android中使用这些api的代码示例吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2011-09-06
    相关资源
    最近更新 更多