高德定位与坐标系转换

  下载定位包并引入

配置权限

  配置AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.XAT"
tools:targetApi="31">
...
<meta-data android:name="com.amap.api.v2.apikey" android:value="您的Key"/>
<service android:name="com.amap.api.location.APSService"/>
...
</application>

  配置权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--允许访问网络,必选权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许获取精确位置,精准定位必选-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--允许获取粗略位置,粗略定位必选-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--允许获取设备和运营商信息,用于问题排查和网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许获取网络状态,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许获取wifi网络信息,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许获取wifi状态改变,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!--后台获取位置信息,若需后台定位则必选-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--用于申请调用A-GPS模块,卫星定位加速-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<!--允许写入扩展存储,用于写入缓存定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<!--允许读设备等信息,用于问题排查-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

请求权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public static final int WRITE_EXTERNAL_STORAGE_CODE = 10001;
public static final int ACCESS_FINE_LOCATION_CODE = 10002;
public static final int READ_PHONE_STATE_CODE = 10003;

/**
* 申请定位权限
*
* @param activity 上下文
*/
public static void requestLocation(Activity activity) {
if (!isLocation(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}, ACCESS_FINE_LOCATION_CODE);
}
}

/**
* 是否拥有定位权限
*
* @param activity 上下文
* @return 结果
*/
public static boolean isLocation(Activity activity) {
return ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

/**
* 读取设备状态权限
*
* @param activity 上下文
*/
public static void requestReadPhoneState(Activity activity) {
if (!isReadPhoneState(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{
Manifest.permission.READ_PHONE_STATE
}, READ_PHONE_STATE_CODE);
}
}

/**
* 是否拥有读取设备状态权限
*
* @param activity 上下文
* @return 结果
*/
public static boolean isReadPhoneState(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
}
return true;
}


/**
* 请求外部储存权限,安卓6.0之前不需要动态申请
*
* @param activity 上下文
*/
public static void requestWrite(Activity activity) {
if (!isWrite(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, WRITE_EXTERNAL_STORAGE_CODE);
}
}

/**
* 是否拥有外部储存写入权限,安卓6.0之前不需要动态申请
*
* @param activity 上下文
* @return 结果
*/
public static boolean isWrite(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
return true;
}

初始化定位

  新建AMapLocation类,initAMapLocation()方法初始化定位,getAMapLocation()获取高德定位点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class AMapLocationUtils {
// 高德地图点位对象
private AMapLocation aMapLocation;

private static AMapLocationUtils AMapLocationUtils;

public static AMapLocationUtils getInstance(){
if (AMapLocationUtils == null){
AMapLocationUtils = new AMapLocationUtils();
}
return AMapLocationUtils;
}

/**
* 初始化
*/
public void initAMapLocation(Context context){
AMapLocationClient.updatePrivacyShow(context, true, true);
AMapLocationClient.updatePrivacyAgree(context, true);

//声明AMapLocationClientOption对象
AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
//声明AMapLocationClient类对象
AMapLocationClient mLocationClient = null;
try {
// 初始化定位
mLocationClient = new AMapLocationClient(context);
} catch (Exception e) {
e.printStackTrace();
}

if(mLocationClient != null){
//设置定位回调监听
mLocationClient.setLocationListener(
aMapLocation -> this.aMapLocation = aMapLocation);

// 设置定位模式为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);

// 设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
mLocationOption.setInterval(1000);

// 设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);

//设置是否允许模拟位置,默认为true,允许模拟位置
mLocationOption.setMockEnable(false);

//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
mLocationOption.setHttpTimeOut(8000);

//关闭定位缓存机制
mLocationOption.setLocationCacheEnable(false);

//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);

//启动定位
mLocationClient.startLocation();

AMapLocationClientOption option = new AMapLocationClientOption();
// 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
mLocationClient.setLocationOption(option);
//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
mLocationClient.stopLocation();
mLocationClient.startLocation();
}
}

public AMapLocation getAMapLocation(){
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
return aMapLocation;
}
else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError","location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
return null;
}
}
return null;
}
}

坐标转换

  通常GPS获取的是WGS84坐标体系,包括很多国外地图厂商提供的地图数据也是WGS84坐标体系,但是在我国,由于政策原因,在地图发布和出版的时候,坐标至少需要经过国家测绘局加密,也就是对WGS84坐标进行一次非线性加偏,得到GCJ02坐标系,俗称火星坐标

  GCJ02转WGS84

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private static final double PI = 3.14159265358979324;
/**
* 将高德地图SDK获取到的GCJ02坐标系转换为WGS84坐标系
* @param lat 维度
* @param lon 经度
* @return 转换后的坐标
*/
public static Location GCJ02ToWGS84(double lat, double lon) {
double a = 6378245.0; //克拉索夫斯基椭球参数长半轴a
double ee = 0.00669342162296594323; //克拉索夫斯基椭球参数第一偏心率平方
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * PI;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);

Location location = new Location("");
location.setLatitude(lat - dLat);
location.setLongitude(lon - dLon);
return location;
}

/**
* 转换经度
*/
public static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}

/**
* 转换纬度
*/
public static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}

参考:Android定位SDK