java 如何获得手机所在地的海拔高度(而不是手机的海拔高度)

ulydmbyx  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(195)

我正在用java实现一个android应用程序来判断一个物体是否在飞行中(在这个例子中,它是一个挂在无人机上的电话)。要继续,我检索电话的高度,然后检索电话所在区域的高度(因为,我假设在该区域的不同地方)。然后我在区域高度上添加定义的高度,以确定当物体到达该高度时是否在飞行中。但问题是,我只能检索到手机的高度,而不能检索到地区的高度,我正在努力计算,你能帮我吗?下面是我实现的代码:

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

    }

}
vvppvyoh

vvppvyoh1#

您正在寻找一个特定位置的标高参考。这需要一个数据库。有这样的服务。例如,在谷歌Map平台,有一个服务称为海拔API,可以给予你地球上任何一点的海拔。你也可以找到一些免费的开放API来帮助你。比如这个。请https://www.opentopodata.org/#host-your-own记住,数据有一个分辨率,比如30米,有些更精细,有些更粗糙.而有些则提供不会更新的静态评估信息。找到一个适合你的要求。

相关问题