-2

I used https://stackoverflow.com/a/5672274/6881568 to find elevation. i am passing longitude and latitude value which i got using GPS (code for which is as below)

public class GPSTracker extends Service implements LocationListener {
private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
double altitude;

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                        altitude = getAltitude(latitude,longitude);
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                            altitude = getAltitude(latitude,longitude);
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }
    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Setting Icon to Dialog
    //alertDialog.setIcon(R.drawable.delete);

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

public double getAltitude(Double longitude, Double latitude) {

    double result = Double.NaN;
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    String url = "https://maps.googleapis.com/maps/api/elevation/"
            + "xml?locations=" + String.valueOf(longitude)
            + "," + String.valueOf(latitude)
            + "&key=AIzaSyD4915zh6cSIB7BAUQI_WZWb-7EOjui8wg";
    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;
}

I am getting wrong results when i tested at different floors of my building as below ,

AT 5th floor - ( latitude = 12.9174253 , longitude = 77.5005331 , elevation = 2636.495278941856 feet )

AT 4th floor - (latitude = 12.9176438 ,longitude = 77.5004883, elev =2634.1930440302203 feet)

AT 3rd floor - (latitude =12.9175136 ,longitude = 77.5005331 ,elev = 2635.651439701728 feet)

this is wrong am getting 2635 at 3rd level and 2634 at 4th level. height should be more at 4th level.at 2nd floor again i got 2636 feet

Community
  • 1
  • 1
praveen
  • 31
  • 6

1 Answers1

0

The Elevation APIs give you the elevation from the surface earth, but not considering the buildings or the indoor. It gives you the elevation of the earth at that location, so if you are on top on a mountain or at the sea level the value will differ, but if you go to the 3rd floor or "jump" you will not get the "miracle" elevation. That service has a kind of "grid" of precalculated elevations, the value of the elevation for the exact same position should be the same across all the floors you are considering.

N Dorigatti
  • 3,482
  • 2
  • 22
  • 33
  • what is elevation first of all, i thought its height from sea level. and what is resolution ? (in that google elevation api). i couldn't understand the one given in its website – praveen Nov 29 '16 at 03:53
  • Actually my requirement is to find altitude, or height of my mobile from earth surface.suggest something for that. – praveen Nov 29 '16 at 04:10
  • The only thing you can do if you dont have any wifi, bluetooth or other reference in the building is to try to use barometer, if phone has it – N Dorigatti Nov 29 '16 at 06:12
  • I have wifi facility in building.every phone will not have barometer and bluetooth is short distance communication so i cant choose both .what can i do with wifi? – praveen Nov 29 '16 at 06:27
  • you can "map" the mac address of wifi access points to the floor in which they belong. Then in the code you scan for WiFi APs and get those macAddresses, the one with better signal (hopefully is the closest one in the same floor), can tell you the floor by looking at the mapping you have made. PS: The mapping has to be done manually! – N Dorigatti Nov 29 '16 at 08:18
  • how it is helpful to get height of my mobile from earth surface ? – praveen Nov 29 '16 at 08:55
  • you can use the elevation API to get the "floor 0" height, and then add the floors height to that. This will always be an approximation, you can't substitute a professional barometer with a phone and some assumptions... – N Dorigatti Nov 29 '16 at 08:59
  • ok thank you :) is it possible to use accelerometer or google earth to find height...? – praveen Nov 29 '16 at 09:29
  • Which height? Of the ground terrain? You can use elevation api for it, if you want Real elevation as told, there is no magic – N Dorigatti Nov 29 '16 at 09:39
  • elevation API only i used to get height. but it is of ground. i want height of mobile if i am on different floors of building. – praveen Nov 29 '16 at 09:49
  • then you need to know the eight of each floor. That is up to you and the building! – N Dorigatti Nov 29 '16 at 10:15