其他分享
首页 > 其他分享> > Android如何在LocationManager中获取高度.我总是零

Android如何在LocationManager中获取高度.我总是零

作者:互联网

我想从location.getAltitude()方法获取当前海拔高度.但它总是返回零.我该怎么做才能获得当前的海拔高度值.但正确地获得经度和格度.请指教.我在谷歌搜索并制定了一些样本,但所有样本只返回零.

我用过的代码:

在on create中我调用了= getMyCurrentLocation();这种方法.

void getMyCurrentLocation() {
        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();
try{gps_enabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
           try{network_enabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}  
if(gps_enabled){

                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

            }

            if(network_enabled){
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);

            }


        if(gps_enabled){
                location=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);



            }

            if(network_enabled && location==null)   {
                location=locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  

            }

        if (location != null) {

            MyLat = location.getLatitude();
            MyLong = location.getLongitude();
    Toast.makeText(this,"My Lat : " + MyLat + "\n" + "My Long : " + MyLong, Toast.LENGTH_SHORT).show();

        } else {

        }


        System.out.println(" LATTITUDE " + MyLat);
        System.out.println("Longitude " + MyLong);

    }


public class MyLocationListener implements LocationListener

        {

        @Override

        public void onLocationChanged(Location loc)

        {

        MyLat =loc.getLatitude();
        MyLong=   loc.getLongitude();
       gooalt=getElevationFromGoogleMaps(MyLat,MyLong) ;
      double alt=loc.getAltitude(); **==>>> This one returns ZERO**

        String Text = "Longitud = " + (loc.getLongitude()) +"Latitud = " + loc.getLatitude()+"Altitude "+gooalt+"feet";

        Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();


        }

        @Override

        public void onProviderDisabled(String provider)

        {

        Toast.makeText( getApplicationContext(),"Gps  Disabled",Toast.LENGTH_SHORT ).show();

        }

        @Override

        public void onProviderEnabled(String provider)

        {

        Toast.makeText( getApplicationContext(),"Gps   Enabled",Toast.LENGTH_SHORT).show();

        }

        @Override

        public void onStatusChanged(String provider, int status, Bundle extras)

        {

        }

        }

解决方法:

你用什么方法来获取位置?
也许你可以试试http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String),这对我有用.

如果没有,也许尝试另一种方法,有几种方法:
http://developer.android.com/guide/topics/location/index.html

顺便说一句:你能给我们一些代码吗?你在测试什么设备?

标签:android,altitude
来源: https://codeday.me/bug/20190709/1415560.html