1. 程式人生 > >Android 獲取LocationProvider以及獲取定位資訊

Android 獲取LocationProvider以及獲取定位資訊

 

獲取LocationProvider的三種方法

一、獲取所有的LocationProvider並用TextView顯示出來

        //獲取顯示LocationProvider名稱的TextView
        providerTv = findViewById(R.id.act_provider_tv);
        //獲取位置服務
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        //這個方法返回值是List集合,裡面資料是String型別的
        List<String> providerNames = locationManager.getAllProviders();//獲取所有的LocationProvider名稱
        StringBuilder stringBuilder = new StringBuilder();//字元構建器
        for ( Iterator<String> iterator = providerNames.iterator();iterator.hasNext();) {
            stringBuilder.append(iterator.next() + "\n");
        }
        providerTv.setText(stringBuilder.toString());

二、通過名稱獲得LocationProvider

        //獲取顯示LocationProvider名稱的TextView
        providerTv = findViewById(R.id.act_provider_tv);
        //獲取位置服務
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        //獲取基於GPS的LocationProvider
        //需要加入許可權  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);

        providerTv.setText(provider.getName());

三、通過Criteria類獲得LocationProvider

        //獲取顯示LocationProvider名稱的TextView
        providerTv = findViewById(R.id.act_provider_tv);
        //獲取位置服務
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        //獲取最佳的LocationProvider
        //建立一個過濾條件物件
        //需要加入許可權  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        Criteria criteria = new Criteria();
        //設定為不收費的
        criteria.setCostAllowed(false);
        //使用精度最準確的
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //設定中等耗電量
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
        //獲取最佳的LocationProvider名稱
        String provider = locationManager.getBestProvider(criteria, true);
        providerTv.setText(provider);

——————————————————————————————————————————————————————————

獲取定位資訊

  • 獲取一個locationManager物件,主要通過getSystemService獲取
  • 設定一個監聽器,用來實現每隔一段時間獲取定位資訊
  • 編寫一個locationUpdates方法,把獲取到的定位資訊進行輸出
  • 獲取locationManager物件的getLastKnownLocation方法獲取最新的定位資訊
  • 傳到locationUpdates方法中
private TextView locationTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);     //設定全屏

        //獲取顯示LocationProvider名稱的TextView
        locationTv = findViewById(R.id.act_title_tv);
        //獲取位置服務
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        //許可權檢查,編輯器自動新增
        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;
        }
        assert locationManager != null;
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,   //指定GPS定位的提供者
                1000,                 //間隔時間
                1,                 //位置間隔1米
                new LocationListener() {//監聽GPS定位資訊是否改變
                    @Override
                    public void onLocationChanged(Location location) {
                    //GPS資訊發生改變時,回撥
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        //GPS狀態發生改變時,回撥
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    //定位提供者啟動時回撥
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        //定位提供者關閉時回撥
                    }
                }
        );
        //獲取最新的定位資訊
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //將最新的定位資訊傳遞給locationUpdates()方法
        locationUpdates(location);
//              需要新增兩個位置許可權
//              近似精度的許可權
//            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//              更精細精度的訪問許可權
//            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    }

    public void locationUpdates(Location location) {
        if (location != null) {
            //建立一個字串構建器,用於記錄位置資訊
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("您的位置是: \n");
            stringBuilder.append("經度: ");
            stringBuilder.append(location.getLongitude());
            stringBuilder.append("\n 緯度:");
            stringBuilder.append(location.getLatitude());
            locationTv.setText(stringBuilder.toString());

        } else {
            locationTv.setText("沒有獲取到GPS資訊");
        }
    }