Mak*_*tar 36 gis android geolocation android-mapview
有没有一种快速有效的方法来获得Android平台上经度和纬度的高度(海拔)?
Mak*_*tar 39
提升应用程序屏幕http://img509.imageshack.us/img509/4848/elevationc.jpg
我的方法是使用USGS Elevation Query Web服务:
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;
}
Run Code Online (Sandbox Code Playgroud)
和使用示例(在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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
小智 23
您还可以使用Google Elevation API.其在线文档位于:https: //developers.google.com/maps/documentation/elevation/
请从以上API页面中注意以下内容:
使用限制:使用Google地理编码API每天的查询限制为2,500个地理定位请求.(Google Maps API Premier的用户每天最多可执行100,000次请求.)此限制的使用是为了防止滥用和/或重新使用地理编码API,此限制可能会在将来更改,恕不另行通知.此外,我们强制执行请求速率限制以防止滥用服务.如果您超过24小时限制或以其他方式滥用服务,Geocoding API可能会暂时停止为您工作.如果您继续超出此限制,则可能会阻止您访问地理编码API.注意:地理编码API只能与Google地图结合使用; 禁止在地图上显示地理编码结果.有关允许使用的完整详细信息,请参阅Maps API服务条款许可限制.
更改Max Gontar上面的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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
54935 次 |
最近记录: |