Android编程获取GPS速度

博主之前设计电动自行车,因为测速电路不好设计,因此采用蓝牙连接主控,手机GPS测速并且授速于硬件CPU。

##GPS测速程序如下 #FF0011

        GPSmessage = (TextView) this.findViewById(R.id.textView4);//实例化控件用于显示
        String serviceName = this.LOCATION_SERVICE;
        //获得位置服务的管理对象
            LocationManager locationManager = (LocationManager)getSystemService(serviceName);
        // 通过GPS获取定位的位置数据
            Criteria criteria = new Criteria();  
            criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度  
            criteria.setAltitudeRequired(false);// 设置不需要获取海拔方向数据  
            criteria.setBearingRequired(false);// 设置不需要获取方位数据  
            criteria.setCostAllowed(true);// 设置允许产生资费  
            criteria.setPowerRequirement(Criteria.POWER_HIGH);// 低功耗  

location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
updateToNewLocation(location);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 500, 1, new LocationListener() {//监听参数500ms更新一次或者1米更新一次
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            public void onProviderEnabled(String provider) {
            }
            public void onProviderDisabled(String provider) {
            }
            public void onLocationChanged(Location location) {
                updateToNewLocation(location);//进入更新程序
            }
        });

更新程序

    private void updateToNewLocation(Location location) {

        if (location != null) {
            double  latitude = location.getLatitude();//维度
            double longitude= location.getLongitude();//经度
            float  speed=location.getSpeed();//取得速度
            DecimalFormat decimalFormat=new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
            String p=decimalFormat.format(speed*3.6);//format 返回的是字符串
            GPSmessage.setText("速度:"+p);
        } else {
            GPSmessage.setText("无法获取地理信息");
        }

    }

猜你喜欢

转载自blog.csdn.net/l420ll/article/details/78789236
今日推荐