【Android笔记 三】Location获取地理位置信息(中)Criteria类的简单使用

上一篇文章 【Android笔记 二】Location获取地理位置信息(上)介绍了简单的地理位置服务的使用,今天这篇文章给大家分享以下Criteria类的使用


在使用android lcoation的时候,可能不希望自己去硬性的选择是GPS服务还是NETWORK服务,可能是我们考虑的因素有很多,自己很难决定,Android SDK提供了一个类Criteria,直译为标准。

注意在locationManager中还有一个重要的方法就是getBestProvider()
------------------------------------------------------------------------------------------------------------------------------------------------
public String getBestProvider (Criteria criteria, boolean enabledOnly)
Since: API Level 1

Returns the name of the provider that best meets the given criteria. Only providers that are permitted to be accessed by the calling activity will be returned. If several providers meet the criteria, the one with the best accuracy is returned. If no provider meets the criteria, the criteria are loosened in the following sequence:

//翻译:返回满足给定的criteria(标准)的最佳provider,(前面一篇文章提到过两种重要provider的区别)。只有当provider权限允许的时候才会返回,如果几个provider均满足criteria设定的标准,最贴近标准的provider将会被返回。如果没有provider满足,标准将按照下面的序列放宽松。
power requirement
accuracy
bearing
speed
altitude

Note that the requirement on monetary cost is not removed in this process.
//在放宽标准的过程中,对于资费的要求不会被移除,我感觉也就是是否允许产生资费的标准不会改变

Parameters
criteria the criteria that need to be matched
enabledOnly if true then only a provider that is currently enabled is returned //这个参数为true的时候,只会返回当前可用的provider,例如GPS处于关闭状态时,就不会获得了
Returns

name of the provider that best matches the requirements


------------------------------------------------------------------------------------------------------------------------------------------------
下面详细的看一下Criteria类的解释

A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
翻译:“这个类表示了应用程序选择位置服务provider的一个标准,provider可能是根据精准度,电量使用,能否获得海拔,速度、方向和产生资费来选择的”也就是说,criteria给用户提供了多种因素的标准设置,locationManager可以根据这个设定好的标准,自动选择最慢组需求的provider。
------------------------------------------------------------------------------------------------------------------------------------------------
void setAccuracy(int accuracy)
Indicates the desired accuracy for latitude and longitude. //设置经纬度的精准度 可选参数有ACCURACY_FINE 准确 ACCURACY_COARSE 粗略
void setAltitudeRequired(boolean altitudeRequired)
Indicates whether the provider must provide altitude information./ /设置是否需要获取海拔数据
void setBearingAccuracy(int accuracy)
Indicates the desired bearing accuracy. //设置方向的精确 可选参数有ACCURACY_LOW,低 ACCURACY_HIGH 高 NO_REQUIREMENT.没有要求
void setBearingRequired(boolean bearingRequired)
Indicates whether the provider must provide bearing information .//设置是否需要获得方向信息
void setCostAllowed(boolean costAllowed)
Indicates whether the provider is allowed to incur monetary cost. //设置是否允许定位过程中产生资费,比如流量等
void setHorizontalAccuracy(int accuracy)
Indicates the desired horizontal accuracy (latitude and longitude). //获取水平方向经纬度的精准度 可选参数ACCURACY_LOW, 低 ACCURACY_MEDIUM 中 ACCURACY_HIGH 高 NO_REQUIREMENT. 无要求
void setPowerRequirement(int level)
Indicates the desired maximum power level. //设置耗电量的级别
void setSpeedAccuracy(int accuracy)
Indicates the desired speed accuracy. //设置速度的精确度
void setSpeedRequired(boolean speedRequired)
Indicates whether the provider must provide speed information. //设置是否提供速度的要求
void setVerticalAccuracy(int accuracy)

Indicates the desired vertical accuracy (altitude).//设置垂直距离的海拔高度



这里是我写的一段演示代码

单击链接打开文章


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio ,可以使用 Google Play Services 提供的 Location API 来获取经纬度。以下是获取用户当前位置经纬度的步骤: 1. 在 AndroidManifest.xml 文件添加以下权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 2. 在 build.gradle(Module:app) 文件添加以下依赖: ```gradle implementation 'com.google.android.gms:play-services-location:18.0.0' ``` 3. 在代码添加以下代码: ```java // 创建LocationManager对象 LocationManager locationManager = (LocationManager) getSystemService(Context.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) { // 请求获取位置信息的权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); return; } // 获取位置提供器名称,GPS或是NetWork Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 criteria.setAltitudeRequired(false); // 不要求海拔 criteria.setBearingRequired(false); // 不要求方位 criteria.setCostAllowed(true); // 允许有花费 criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 String provider = locationManager.getBestProvider(criteria, true); // 获取LocationProvider // 获取位置信息 Location location = locationManager.getLastKnownLocation(provider); if (location != null) { double latitude = location.getLatitude(); // 获取纬度 double longitude = location.getLongitude(); // 获取经度 Log.d("TAG", "latitude: " + latitude); Log.d("TAG", "longitude: " + longitude); } ``` 注意:需要在 Android 手机上打开定位功能才能获取到位置信息。如果获取不到位置信息,可以尝试在设置打开定位服务。同时,获取位置信息可能会比较耗时,建议在子线程执行,避免阻塞主线程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值