public class LocalUtil {
private Context mContext;
private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
public static double longitude = 0;
public static double latitude = 0;
private boolean isRefreshAnim = false; // 是否在执行刷新动画
private OnItemClickListener mOnItemClickListener = null;
public LocalUtil(Context context) {
this.mContext = context;
}
public void startLocation() {
//初始化client
locationClient = new AMapLocationClient(mContext);
locationOption = getDefaultOption();
//设置定位参数
locationClient.setLocationOption(locationOption);
// 设置定位监听
locationClient.setLocationListener(locationListener);
// 设置定位参数
locationClient.setLocationOption(locationOption);
// 启动定位
locationClient.startLocation();
}
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
//如果网络可用就选择高精度
if (Network.isAvailable(mContext)) {
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(true);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
}
//否则就选择仅设备模式
else {
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
}
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setSensorEnable(true);//可选,设置是否使用传感器。默认是false
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
return mOption;
}
AMapLocationListener locationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
StringBuilder sb = new StringBuilder();
if (null != location) {
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if (location.getErrorCode() == 0) {
if (isRefreshAnim) {
ToastUtil.showToastLong("获取实时位置成功!");
}
longitude = location.getLongitude();
latitude = location.getLatitude();
String address = location.getAddress();
locationSuccess(longitude, latitude, location, address);
//定位完成的时间
} else {
//定位失败
sb.append("定位失败" + "\n");
sb.append("错误码:" + location.getErrorCode() + "\n");
sb.append("错误信息:" + location.getErrorInfo() + "\n");
sb.append("错误描述:" + location.getLocationDetail() + "\n");
LocationFarile("获取失败", location);
if (isRefreshAnim) {
ToastUtil.showToastLong("获取实时位置失败!");
}
}
} else {
LocationFarile("获取失败", location);
if (isRefreshAnim) {
ToastUtil.showToastLong("获取实时位置失败!");
}
}
}
};
private void LocationFarile(String count, AMapLocation location) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(0, 0, location, count, "");
}
}
//dingw
public void locationSuccess(double longitude, double latitude, AMapLocation location, String address) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(longitude, latitude, location, "", address);
}
}
public interface OnItemClickListener {
void onItemClick(double longitude, double latitude, AMapLocation location, String count, String address);
// void onItemClick(double longitude, double latitude, AMapLocation location,String count);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
调用方式
LocalUtil localUtil = new LocalUtil(getActivity());
localUtil.startLocation();
localUtil.setOnItemClickListener(new LocalUtil.OnItemClickListener() {
@Override
public void onItemClick(double longitude, double latitude, AMapLocation location, String count, String address) {
textEdit.setText(longitude + "");
item.setDisplayValue(longitude + "");
}
});