Get Altitude by Longitude and Latitude in Android

Get altitude by longitude and latitude in Android

My approach is to use 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;
}

And example of use (right in HelloMapView class):

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

How to get Altitude from Latitude and Longitude or Google Map in Android?

You can make an HTTP call to the Google Maps Elevation API, documented at https://developers.google.com/maps/documentation/elevation/intro. I don't believe that there is native Android client library for this API. You can make a request including latitude/longitude and receive elevation information in the response.

Require altitude from latitude and longitude

I have tried the Below Way in my application for getting Altitude from Lat/Long. you can try it out if it helps you.

private double getAltitudeFromLatLong(Double lat, Double long) {
double result = 0.0;
HttpClient httpClient = new DefaultHttpClient();
HttpContext Context = new BasicHttpContext();
String URL = "http://gisdata.usgs.gov/"
+ "xmlwebservices2/elevation_service.asmx/"
+ "getElevation?X_Value=" + String.valueOf(long)
+ "&Y_Value=" + String.valueOf(lat)
+ "&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true";
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet, Context);
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 tag1 = "<double>";
String tag2 = "</double>";
if (respStr.indexOf(tag1) != -1) {
int start = respStr.indexOf(tag1) + tag1.length();
int end = respStr.indexOf(tag2);
String value = respStr.substring(start, end);
result = Double.parseDouble(value);
}
instream.close();
}
}
catch (Exception e) {}
return result;
}

How to determine my current location (longitude,latitude and altitude)?

First Enable Ur GPS.

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

public static double lontitube,latitude;

protected LocationManager locationManager;
protected Location currentLocation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
}

protected void performReverseGeocodingInBackground() {
showCurrentLocation();

}

protected void showCurrentLocation() {

currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (currentLocation != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
currentLocation.getLongitude(), currentLocation.getLatitude()

);
lontitube=currentLocation.getLongitude();
latitude=currentLocation.getLatitude();

System.out.println("----lati----longi----"+latitude+"\n"+lontitube);

Toast.makeText(Demo.this, message,
Toast.LENGTH_LONG).show();
}

}

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location) {

String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(Demo.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}

public void onProviderDisabled(String s) {
Toast.makeText(Demo.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(Demo.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}

}

In AndroidManifest add

ACCESS_FINE_LOCATION

permission for GPS use.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Android how to get altitude in LocationManager. i am alwaya getting zero

What method did u used to get the location?
Maybe u could try http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String) instead, that worked for me.

If not, maybe try another approach, there are several ones:
http://developer.android.com/guide/topics/location/index.html

BTW: could u give us some code? On what device are you testing?



Related Topics



Leave a reply



Submit