How get the altitude of a place where the phone is located ( not the altitude of the phone)

141 Views Asked by At

I am implementing an android app in java to determine if an object is in flight (in the example ,it's a phone hang on a drone ) . To proceed,i retrieve the altitude of the phone , then i retrieve the altitude of the area where the phone is ( because , i suppose that in the different places in the area).Then i add a defined height on the area altitude to determine if the object is in flight when it reaches this height . But the problem is , the only altitude i can retrieve is the phone altitude but not the area altitude and i'm trying to figure it out .Could you help me ? Here is the code , i've implemented :

public class TimeService extends Service{
    private final static String TAG = TimeService.class.getName();
    
  LocationEntity location = new LocationEntity();
   
   private  Handler handler;

    FusedLocationProviderClient fusedLocationProviderClient;
   
    LocationRequest locationRequest;
   
    

    LocationRequest newlocationRequest;
   

    double Longitude, Latitude, Altitude,Acccuracy,Speed;
  
    AppDatabase appDatabase;

    @Override
    public void onCreate() {
        super.onCreate();
        context=this;
      
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
         appDatabase=AppDatabase.getDbIntance(this);
      

        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(@NonNull LocationResult locationResult) {
                super.onLocationResult(locationResult);

                Location location = locationResult.getLastLocation();
                assert location != null;
                Log.d("TAG", "onLocationResult: callback method  "+"longitude "+location.getLongitude()+"latitude "+location.getLatitude()+"Accuracy "+location.getAccuracy()+"Speed "+location.getSpeed());
            }
        };

        startLocationUpdate();
       
        handler = new Handler();

        // Define the code block to be executed
         Runnable runnable = new Runnable() {
            @Override
            public void run() {
                // Insert custom code here
                   startLocationUpdate();
                Log.d("TAG", "run: "+"run every 30s");
                // Repeat every 2 seconds
                handler.postDelayed(this, 30000L);
            }
        };

// Start the Runnable immediately
        handler.post(runnable);
    }
}

    //Location function
    //Setting all properties of Location

    public void locationSettings() {
//Put in the time service
        newlocationRequest = new LocationRequest();
        newlocationRequest.setInterval(30000);
        newlocationRequest.setFastestInterval(5000);
        //most accurate use GPS
        newlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    public void updateGPS() {

       // fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        //user provided the permission , run the code
        BaseView baseView = new BaseView();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermi ssions for more details.
            return;
        }
        fusedLocationProviderClient.getLastLocation().addOnSuccessListener(baseView, position -> {
            Log.d("TAG", "updateGPS: position elements are : " + "lon = " + position.getLongitude() + "lat = " + position.getLatitude()+"alt "+position.getAltitude() + "Acc= " + position.getAccuracy() + " Speed = " + position.getSpeed());


                location.setLongitude(position.getLongitude());
                location.setLatitude(position.getLatitude());
                location.setAltitude(position.getAltitude());
                location.setAccuracy(position.getAccuracy());
                location.setSpeed(position.getSpeed());

            assert location!=null;
            appDatabase.locationDao().insert(location);

        });

    }



    public void startLocationUpdate() {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        fusedLocationProviderClient.requestLocationUpdates(newlocationRequest,locationCallback, null);
        updateGPS();


    }

}
1

There are 1 best solutions below

1
Ruineie On

You are looking for a reference of the elevation of a certian location. This needs a database. There are such services. For example, in google map platform, there is a service called elevation api which can give you elevation of any point on earth. You can also find some free open API that can help you. For example this one. https://www.opentopodata.org/#host-your-own Remember, he data has a resolution, like 30m, some are finer, while some are coarser. And some provides static evelation information that won't be updated. Find the one suits your requirments.