106.android简单的GPS实时定位

106.android简单的GPS实时定位_第1张图片

 

//定位权限、网络权限:



 

//GPS开关和app定位权限手动打开,这里未提供。以下初始化GPS定位代码和定位实时监听代码:

 

 /**
     * GPS定位初始化
     */
    private void locationGPS() {
        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;
        }

        //绑定监听,有4个参数
        //参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
        //参数2,位置信息更新周期,单位毫秒
        //参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
        //参数4,监听
        //备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
        // 1秒更新一次,参数3为0,则通过时间来定时更新。
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
    }

    /**
     * GPS位置实时监听
     */
    private LocationListener locationListener = new LocationListener() {

        /**
         * 位置信息变化时触发
         */
        @Override
        public void onLocationChanged(final Location location) {
            Log.i("TAG", "时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(location.getTime()));
            Log.i("TAG", "经度:" + location.getLongitude());
            Log.i("TAG", "纬度:" + location.getLatitude());
            Log.i("TAG", "海拔:" + location.getAltitude());

            //GPS计算距离,指定一个location1地址
//            Location location1 = new Location("l1");
//            location1.setLongitude(116.200000);
//            location1.setLatitude(40.200000);
//            //定位地址location调用distanceTo,计算到location1的距离
//            float v = location.distanceTo(location1);
//            Log.e("TAG", "本次定位地址距离location1:" + (int) v);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Geocoder geocoder = new Geocoder(MyApp.mAppContent, Locale.getDefault());
                        List
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses.size() > 0) { Address address = addresses.get(0); if (address != null) { String locationAddr = address.getAddressLine(0).replace("Unnamed Road", ""); Log.e("TAG", "locationAddr:" + locationAddr); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); } /** * GPS状态变化时触发 */ @Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { //GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i("TAG", "当前GPS状态为可见状态"); break; //GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i("TAG", "当前GPS状态为服务区外状态"); break; //GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i("TAG", "当前GPS状态为暂停服务状态"); break; } } /** * GPS开启时触发 */ @Override public void onProviderEnabled(String provider) { if (ActivityCompat.checkSelfPermission(MyApp.mAppContent, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MyApp.mAppContent, 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; } } /** * GPS禁用时触发 */ @Override public void onProviderDisabled(String provider) { } };

//---------------------------------------------------------------------------------------完---------------------------------------------------------------------------------

你可能感兴趣的:(android原生控件,android权限)