GPS技术实现定位功能

GPS技术实现定位功能

1.加权限
2LocationManger manger=getSystemService(Locationg_Service)
获取位置管理类
3 Location 位置类
getLaatitale()获取纬度值
getLongtitude() 获取经度值
getAktutyde() 获得海拔
当位置发生改变时获得地址信息 调用locationManager中的方法
requestlocationUpdate 位置提供器位置变化时间间隔
位置变化的距离的间隔 时间监听LocationListener

代码

	  LocationManager manager;
    String[] permissions;
    boolean hasnoPermisson;
    boolean isOK = false;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        permissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
        manager = (LocationManager) getSystemService(LOCATION_SERVICE);
        initPermission();
        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;
        }
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void initPermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (String per : permissions) {
                if (checkSelfPermission(per) != PackageManager.PERMISSION_GRANTED) {
                    hasnoPermisson = true;
                    break;
                }
            }
        }
        if (hasnoPermisson) {
            requestPermissions(permissions, 100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    isOK = true;
                    break;
                }
            }
        }
        if (isOK) {
            Log.e("error", "权限没申请成功");
        }
    }

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.e("###经度是:", location.getLatitude() + "");
            Log.e("###维度是:", location.getLongitude() + "");
            Log.e("###海拔是:", location.getAltitude() + "");
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

你可能感兴趣的:(Android学习)