1. User Location的关键API
Location Manager:用于管理android的用户定位服务
Location Providers:提供多种定位方式供开发者选择
2. 定位方式的分类
1. GPS定位
使用GPS卫星进行定位,需要在AndroidManifest.xml中声明权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2. NETWORK定位
使用信号接收塔和wifi介入点进行定位,需要在AndroidManifest.xml中声明权限android.permission.ACCESS_FINE_LOCATION 或 android.permission.ACCESS_COARSE_LOCATION
3. 获取用户的当前位置
- 在AndroidManifest.xml中声明相应的权限
- 获取LocationManager对象
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
3. 选择LocationProvider
4. 绑定LocationListener对象
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5000, new MyLocationListener());
MyLocationListener需要实现LocationListener接口
4.获取最佳的Location Provider
通过一些条件查询合适的location provider对象
public void setAccuracy (int accuracy) 设置精度 ACCURACY_FINE和ACCURACY_COARSE
public void setAltitudeRequired (boolean altitudeRequired)是否需要提供海拔
public void setBearingAccuracy (int accuracy)设置方向的精度, ACCURACY_LOW, ACCURACY_HIGH, NO_REQUIREMENT
public void setCostAllowed (boolean costAllowed)是否产生费用
public void setHorizontalAccuracy (int accuracy)设置水平方向的精度
public void setPowerRequirement (int level)设置电量消耗的级别,NO_REQUIREMENT, POWER_LOW, POWER_MEDIUM, or POWER_HIGH.
public void setSpeedAccuracy (int accuracy)设置速度的精度
public void setSpeedRequired (boolean speedRequired)是否需要速度
public void setVerticalAccuracy (int accuracy)设置垂直方向的精度