下载定位包并引入

配置权限
配置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" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <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;
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); } }
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; }
public static void requestReadPhoneState(Activity activity) { if (!isReadPhoneState(activity)) { ActivityCompat.requestPermissions(activity, new String[]{ Manifest.permission.READ_PHONE_STATE }, READ_PHONE_STATE_CODE); } }
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; }
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); } }
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 mLocationOption = new AMapLocationClientOption(); AMapLocationClient mLocationClient = null; try { mLocationClient = new AMapLocationClient(context); } catch (Exception e) { e.printStackTrace(); }
if(mLocationClient != null){ mLocationClient.setLocationListener( aMapLocation -> this.aMapLocation = aMapLocation);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setOnceLocationLatest(true);
mLocationOption.setInterval(1000);
mLocationOption.setNeedAddress(true);
mLocationOption.setMockEnable(false);
mLocationOption.setHttpTimeOut(8000);
mLocationOption.setLocationCacheEnable(false);
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
AMapLocationClientOption option = new AMapLocationClientOption(); option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn); mLocationClient.setLocationOption(option); mLocationClient.stopLocation(); mLocationClient.startLocation(); } }
public AMapLocation getAMapLocation(){ if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { return aMapLocation; } else { 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;
public static Location GCJ02ToWGS84(double lat, double lon) { double a = 6378245.0; 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