最近项目用到了定位获取位置信息,研究了下,特此记录;
注意:每个应用都需要申请一个单独的key值,因为包名不一样,debug和release 的SHA1的值在E:\Project\JNASProject\高德定位官方Demo文件夹中都有,查找即可;
高端官网:https://lbs.amap.com/dev/key/app 里面有教程和步骤
清单文件
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="222d3ea0799683826f9697455ba87f4b" //这个值每个应用都要去高德平台去申请注册一个
/>
在application标签中声明service组件:
XML
<service android:name="com.amap.api.location.APSService"></service>
这里Value自己填写申请的key
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
public String getLatitude() {
// 初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
// 设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
// 初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
// 设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// 设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(3000);
// 给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
// 启动定位
mLocationClient.startLocation();
/**
* 获取一次定位
*/
// 该方法默认为false,true表示只定位一次
mLocationOption.setOnceLocation(true);
return null;
}
// 声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
// TODO Auto-generated method stub
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
// 可在其中解析amapLocation获取相应内容。
// double locationType =
// amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
String latitude = "+" + Double.toString(amapLocation.getLatitude());// 获取纬度
String latLong = Double.toString(amapLocation.getLongitude());// 获取经度
Log.d("11111111111111", Double.toString(amapLocation.getAccuracy()));
Log.e("Amap==经度:纬度", "locationType:" + latitude + ",latLong:" + latLong);
amapLatBean = new AmapLatBean(latitude, latLong);
Log.d("amapLatBean", amapLatBean.getLatitude());
Log.d("amapLatBean", amapLatBean.getLatLong());
} else {
// 定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
if (mLocationClient.isStarted()) {
// 获得位置之后停止定位
mLocationClient.stopLocation();
}
}
}
}
};
/**
* Created by ZFR on 2019/4/22.
*/
public class AmapLatBean {
String latitude;
String latLong;
public AmapLatBean(String latitude, String latLong) {
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLatLong() {
return latLong;
}
public void setLatLong(String latLong) {
this.latLong = latLong;
}
}
另附获取属性分别为:
如需源码或有啥问题请回复,有完整的demo可提供;
注:如有问题可以回复,看到第一时间分析解决,码农不易,感觉对您有用,帮助到您,可否打赏一杯可乐,在此谢过诸位,愿诸君终成大神,前程似锦~~~