目录
入门指南:
开发指南
1、获取项目key值
2、集成高德SDK
3、获取定位数据
3.1、配置AndroidManifest.xml
3.2、初始化定位
3.3、配置参数并启动定位
3.4、工具类:LocationUtils.java
3.5、使用:
入门指南:
https://lbs.amap.com/api/android-location-sdk/gettingstarted
开发指南
https://lbs.amap.com/api/android-location-sdk/guide/create-project/android-studio-create-project
1、获取项目key值
进入控制台按照步骤操作就行。获取SHA1值可参考这篇文章。
2、集成高德SDK
方法一:拷贝 jar 文件至 libs 文件夹下
方法二:通过Gradle集成SDK
3、获取定位数据
3.1、配置AndroidManifest.xml
首先,声明Service组件并设置高德Key
请在application标签中声明service组件,每个app拥有自己单独的定位service。
<!-- 高德地图 start -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="1dae7f0c5c1eed9d3919545a023d894e" />
<service android:name="com.amap.api.location.APSService" />
<!-- 高德地图 end -->
然后,声明权限
Android 6.0及以上系统可以参考Android 6.0权限说明章节。
<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
3.2、初始化定位
请在主线程中声明AMapLocationClient类对象,需要传Context类型的参数。推荐用getApplicationContext()方法获取全进程有效的context。
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener();
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
3.3、配置参数并启动定位
创建AMapLocationClientOption对象
AMapLocationClientOption对象用来设置发起定位的模式和相关参数。
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
更详细请参考:https://lbs.amap.com/api/android-location-sdk/guide/android-location/getlocation
3.4、工具类:LocationUtils.java
/**
* 类描述:定位工具类 基于高德地图
*/
public class LocationUtil {
//1、保存唯一实例
private volatile static LocationUtil mInstance;
//声明AMapLocationClient类对象
private AMapLocationClient mLocationClient = null;
//声明AMapLocationClientOption对象
private AMapLocationClientOption mLocationOption = null;
private OnLocationListener onLocationListener;
private OnLocationErrorListener onLocationErrorListener;
private getLonLatListener getLonLatListener;
//2、屏蔽外部的new
private LocationUtil() {
getLocation();
}
//3、提供一个全局访问点
public static LocationUtil getInstance() {
if (mInstance == null) {
synchronized (LocationUtil.class) {
if (mInstance == null) {
mInstance = new LocationUtil();
}
}
}
return mInstance;
}
public void setOnLocationListener(OnLocationListener onLocationListener) {
this.onLocationListener = onLocationListener;
}
public void setOnLocationErrorListener(OnLocationErrorListener onLocationErrorListener) {
this.onLocationErrorListener = onLocationErrorListener;
}
public void setgetLonLatListener(LocationUtil.getLonLatListener getLonLatListener) {
this.getLonLatListener = getLonLatListener;
}
//声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//可在其中解析amapLocation获取相应内容。
String address = aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
// HnPrefUtils.setString(PrefKey.Location.ADDRESS, address);
String country = aMapLocation.getCountry();//国家信息
// HnPrefUtils.setString(PrefKey.Location.COUNTRY, country);
String province = aMapLocation.getProvince();//省信息
// HnPrefUtils.setString(PrefKey.Location.PROVINCE, province);
String city = aMapLocation.getCity();//城市信息
// HnPrefUtils.setString(PrefKey.Location.CITY, city);
String district = aMapLocation.getDistrict();//城区信息
// HnPrefUtils.setString(PrefKey.Location.COUNTY, district);
String street = aMapLocation.getStreet();//街道信息
// HnPrefUtils.setString(PrefKey.Location.STREET, street);
String streetNum = aMapLocation.getStreetNum();//街道门牌号信息
// HnPrefUtils.setString(PrefKey.Location.STREET_NUM, streetNum);
LogUtils.d(
"address:------" + address
+ "-----country:------" + country
+ "-----province:------" + province
+ "-----city:------" + city
+ "-----district:------" + district
+ "-----street:------" + street
+ "-----streetNum:------" + streetNum
);
if (onLocationListener != null) {
onLocationListener.onLocationListener(address, country, province, city,
district, street, streetNum, aMapLocation.getLatitude(), aMapLocation.getLongitude());
}
} else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
LogUtils.e("location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
if (onLocationErrorListener != null) {
onLocationErrorListener.onLocationErrorListener(aMapLocation);
}
}
}
mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁
mLocationClient.onDestroy();//销毁定位客户端,同时销毁本地定位服务。
}
};
public void getLocation() {
//初始化定位
mLocationClient = new AMapLocationClient(BaseApp.getContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
// mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
//设置定位模式为AMapLocationMode.Device_Sensors,仅设备模式。
// mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors);
//获取一次定位结果:
//该方法默认为false。
mLocationOption.setOnceLocation(true);
//获取最近3s内精度最高的一次定位结果:
//设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。
// 如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
mLocationOption.setOnceLocationLatest(true);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
public interface OnLocationListener {
void onLocationListener(String address, String country, String province, String city,
String district, String street, String streetNum, double latitude, double longitude);
}
public interface OnLocationErrorListener {
void onLocationErrorListener(AMapLocation aMapLocation);
}
public interface getLonLatListener {
void getLatLon(double longititude, double latitude);
}
}
3.5、使用:
private void getLocation() {
UIHelper.showLoadDialog(this);
LocationUtil.getInstance().setOnLocationListener(new LocationUtil.OnLocationListener() {
@Override
public void onLocationListener(String address, String country, String province,
String city, String district, String street,
String streetNum, double latitude, double longitude) {
UIHelper.closeLoadDialog();
tvLongitude.setText("经度:" + longitude);
tvLatitude.setText("纬度:" + latitude);
tvAddress.setText(address);
}
});
LocationUtil.getInstance().setOnLocationErrorListener(new LocationUtil.OnLocationErrorListener() {
@Override
public void onLocationErrorListener(AMapLocation aMapLocation) {
if (aMapLocation.getErrorCode() == 12) {
UIHelper.closeLoadDialog();
Toast.makeText(MainActivity.this, "请打开GPS定位",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "定位失败",
Toast.LENGTH_SHORT).show();
}
}
});
}