42.Android LocationManager
16lz
2021-01-23
42.Android LocationManager
- Android LocationManager
- LocationManager 介绍
- LocationManager 获取
- LocationListener 初始化
- LocationManager 添加监听
- LocationManager 取得所有Provider
- LocationManager 匹配合适Provider
- LocationManager 测试代码
LocationManager 介绍
LocationManager 提供了访问系统位置服务,这些服务允许应用程序能够获得定期更新设备的地理位置。
由于 LocationManager 属于一种系统服务类型,所以还是需要通过:Context.getSystemService(Context.LOCATION_SERVICE)
于此同时,还要在AndroidManifest.xml里配置如下权限:
- 精确位置权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 粗糙位置权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
LocationManager 获取
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener 初始化
LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { /** * 经度 * 纬度 * 海拔 */ Log.i(TAG, "Longitude:" + Double.toString(location.getLongitude())); Log.i(TAG, "Latitude:" + Double.toString(location.getLatitude())); Log.i(TAG, "getAltitude:" + Double.toString(location.getAltitude())); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { }};
LocationManager 添加监听
在LocationManager的源码中有这段:
/** * Name of the network location provider. * <p>This provider determines location based on * availability of cell tower and WiFi access points. Results are retrieved * by means of a network lookup. */ public static final String NETWORK_PROVIDER = "network"; /** * Name of the GPS location provider. * * <p>This provider determines location using * satellites. Depending on conditions, this provider may take a while to return * a location fix. Requires the permission * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}. * * <p> The extras Bundle for the GPS location provider can contain the * following key/value pairs: * <ul> * <li> satellites - the number of satellites used to derive the fix * </ul> */ public static final String GPS_PROVIDER = "gps"; /** * A special location provider for receiving locations without actually initiating * a location fix. * * <p>This provider can be used to passively receive location updates * when other applications or services request them without actually requesting * the locations yourself. This provider will return locations generated by other * providers. You can query the {@link Location#getProvider()} method to determine * the origin of the location update. Requires the permission * {@link android.Manifest.permission#ACCESS_FINE_LOCATION}, although if the GPS is * not enabled this provider might only return coarse fixes. */ public static final String PASSIVE_PROVIDER = "passive";
有这三种Provider:
NETWORK_PROVIDER
GPS_PROVIDER
PASSIVE_PROVIDER
LocationManager.requestLocationUpdates
public void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
provider:LocationManager的类型(上述的三种填写一个)
minTime:两次定位的最小时间间隔(最少多少秒更新一次)
minDistance:两次定位的最小距离(最少走多远就更新一次)
listener:LocationListener 实例
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
LocationManager 取得所有Provider
LocationManager.getAllProviders
public List<String> getAllProviders()
LocationManager 匹配合适Provider
这里要用到一个类 - Criteria。
/** * 获取以下条件下,最合适的provider */private void getBestProvider() { Criteria criteria = new Criteria(); // 精度高 criteria.setAccuracy(Criteria.ACCURACY_FINE); // 低消耗 criteria.setPowerRequirement(Criteria.POWER_LOW); // 海拔 criteria.setAltitudeRequired(true); // 速度 criteria.setSpeedRequired(true); // 费用 criteria.setCostAllowed(false); String provider = locationManager.getBestProvider(criteria, false); //false是指不管当前适配器是否可用 this.bestProviderTV.setText(provider);}
LocationManager 测试代码
LocationManagerActivity
public class LocationManagerActivity extends AppCompatActivity { private static final String TAG = "LocationManagerActivity"; private LocationManager locationManager; private TextView longitudeTV; private TextView latitudeTV; private TextView altitudeTV; private TextView providersTV; private TextView bestProviderTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_location_manager); this.initViews(); this.locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { /** * 经度 * 纬度 * 海拔 */ Log.i(TAG, "Longitude:" + Double.toString(location.getLongitude())); Log.i(TAG, "Latitude:" + Double.toString(location.getLatitude())); Log.i(TAG, "getAltitude:" + Double.toString(location.getAltitude())); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); this.longitudeTV.setText(location.getLongitude() + ""); this.latitudeTV.setText(location.getLatitude() + ""); this.altitudeTV.setText(location.getAltitude() + ""); this.getProviders(); this.getBestProvider(); } /** * 获取全部的provider */ private void getProviders() { String providers = ""; for (String provider : this.locationManager.getAllProviders()) { providers += provider + " "; } this.providersTV.setText(providers); } /** * 获取以下条件下,最合适的provider */ private void getBestProvider() { Criteria criteria = new Criteria(); // 精度高 criteria.setAccuracy(Criteria.ACCURACY_FINE); // 低消耗 criteria.setPowerRequirement(Criteria.POWER_LOW); // 海拔 criteria.setAltitudeRequired(true); // 速度 criteria.setSpeedRequired(true); // 费用 criteria.setCostAllowed(false); String provider = locationManager.getBestProvider(criteria, false); //false是指不管当前适配器是否可用 this.bestProviderTV.setText(provider); } private void initViews() { this.longitudeTV = (TextView) this.findViewById(R.id.location_longitude_tv); this.latitudeTV = (TextView) this.findViewById(R.id.location_latitude_tv); this.altitudeTV = (TextView) this.findViewById(R.id.location_altitude_tv); this.providersTV = (TextView) this.findViewById(R.id.location_providers_tv); this.bestProviderTV = (TextView) this.findViewById(R.id.location_best_provider_tv); }}
更多相关文章
- Android百度地图——根据城市名,地址名获取GPS纬度、经度值
- Android初始化OpenGL ES,并且分析Renderer子线程原理
- android初始化activity时隐藏软键盘
- Android——编译系统初始化设置
- android初始化部分:how to java2Cpp
- Android开发者已经度过了初级、中级,如何成为一个Android高手呢?
- Android中动态初始化布局参数以及ConstraintLayout使用中遇到的
- Android百度地图——在地图上标注已知GPS纬度经度值的一个或一组
- Android GPS学习笔记—LMS初始化