42.Android LocationManager
来源:互联网 发布:sql 存储过程删除 编辑:程序博客网 时间:2024/05/21 11:34
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); }}
0 0
- 42.Android LocationManager
- Android--LocationManager
- Android LocationManager 使用
- Android LocationManager 使用
- Android LocationManager 使用
- Android LocationManager 使用
- Android 位置管理器(LocationManager)
- android LocationManager的使用
- android locationManager定位
- android:LocationManager详解(-)
- Android系统服务--LocationManager
- Android系统服务:LocationManager
- android locationManager定位
- Android LocationManager 定位服务
- LocationManager
- LocationManager
- LocationManager
- LocationManager
- 应试教育
- 一步一步 搞定RSA(公钥、私钥)
- 纯CSS气泡效果
- JNI Types and Data Structures----JAVA和C语言 c++之间转换的对照表
- Android项目文件构成(Eclipse示例)
- 42.Android LocationManager
- Educational Codeforces Round 2D. Area of Two Circles' Intersection(计算几何+圆相交的面积)
- C++底层数据结构实现
- Android-Android开发者文档阅读1-Device Compatibility
- EXT.NET version2.2 使用时的一些小知识
- 如何实现“返回顶部”的页面效果
- POJ 1125 Stockbroker Grapevine 最短路folyd算法
- C++底层数据结构常用方法
- C语言闰年判断函数