0

次のコードを使用して、地図上の場所をクリックするだけで座標と高度を取得します。たとえば、ニューヨークストリートをクリックすると、アプリからニューヨークの座標が返されます。

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を返します。

この問題を解決する方法はありますか?

4

1 に答える 1

2

マッピングデータには、高度情報は含まれていません。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;
}
}
于 2012-10-22T08:32:57.843 に答える