Android编程获取GPS速度

编程入门 行业动态 更新时间:2024-10-08 13:31:07

Android编程获取GPS<a href=https://www.elefans.com/category/jswz/34/1763015.html style=速度"/>

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("无法获取地理信息");}}

更多推荐

Android编程获取GPS速度

本文发布于:2024-02-14 05:25:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1762420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   Android   GPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!