package com.example.basecore.amap;
|
|
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import android.location.Address;
|
import android.location.Criteria;
|
import android.location.Geocoder;
|
import android.location.Location;
|
import android.location.LocationListener;
|
import android.location.LocationManager;
|
import android.os.Bundle;
|
import android.text.TextUtils;
|
import android.util.Log;
|
|
import com.amap.api.location.AMapLocation;
|
import com.amap.api.location.AMapLocationClient;
|
import com.amap.api.location.AMapLocationClientOption;
|
import com.amap.api.location.AMapLocationListener;
|
|
import java.io.IOException;
|
import java.util.List;
|
|
|
public class LocationProvider implements LocationListener {
|
AMapLocationClient locationClient;
|
public interface LocationGetListener {
|
void getLocationInfo(Location location);
|
}
|
|
public LocationProvider(Context mContext) {
|
this.mContext = mContext;
|
initProvider();
|
}
|
|
Context mContext;
|
LocationManager mLocationManager;
|
LocationGetListener mListener;
|
|
void initProvider() {
|
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
|
|
try {
|
locationClient = new AMapLocationClient(mContext);
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
|
}
|
|
|
public void getLocation(ILocationCallBack callBack) {
|
|
AMapLocationClientOption locationClientOption = new AMapLocationClientOption();
|
//使用单次定位
|
locationClientOption.setOnceLocation(true);
|
// 地址信息
|
locationClientOption.setNeedAddress(true);
|
|
//给定位客户端对象设置定位参数
|
locationClient.setLocationOption(locationClientOption);
|
|
locationClient.setLocationListener(new AMapLocationListener() {
|
@Override
|
public void onLocationChanged(AMapLocation location) {
|
if (location.getErrorCode() == 0) {
|
callBack.onLocationSuccess(location.getAddress(), location.getLatitude(), location.getLongitude());
|
} else {
|
callBack.onLocationFailed(location.getErrorInfo());
|
}
|
}
|
});
|
//启动定位
|
locationClient.startLocation();
|
}
|
|
/**
|
* 停止单次客户端
|
*/
|
public void stopSingleLocation(){
|
if(null != locationClient){
|
locationClient.stopLocation();
|
}
|
}
|
/**
|
* 根据定位结果返回定位信息的字符串
|
* @param location
|
* @return
|
*/
|
public synchronized static String getLocationStr(AMapLocation location){
|
if(null == location){
|
return null;
|
}
|
StringBuffer sb = new StringBuffer();
|
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
|
if(location.getErrorCode() == 0){
|
sb.append("定位成功" + "\n");
|
sb.append("定位类型: " + location.getLocationType() + "\n");
|
sb.append("经 度 : " + location.getLongitude() + "\n");
|
sb.append("纬 度 : " + location.getLatitude() + "\n");
|
sb.append("精 度 : " + location.getAccuracy() + "米" + "\n");
|
sb.append("提供者 : " + location.getProvider() + "\n");
|
|
sb.append("海 拔 : " + location.getAltitude() + "米" + "\n");
|
sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n");
|
sb.append("角 度 : " + location.getBearing() + "\n");
|
if (location.getProvider().equalsIgnoreCase(
|
android.location.LocationManager.GPS_PROVIDER)) {
|
// 以下信息只有提供者是GPS时才会有
|
// 获取当前提供定位服务的卫星个数
|
sb.append("星 数 : "
|
+ location.getSatellites() + "\n");
|
}
|
|
//逆地理信息
|
sb.append("国 家 : " + location.getCountry() + "\n");
|
sb.append("省 : " + location.getProvince() + "\n");
|
sb.append("市 : " + location.getCity() + "\n");
|
sb.append("城市编码 : " + location.getCityCode() + "\n");
|
sb.append("区 : " + location.getDistrict() + "\n");
|
sb.append("区域 码 : " + location.getAdCode() + "\n");
|
sb.append("地 址 : " + location.getAddress() + "\n");
|
sb.append("兴趣点 : " + location.getPoiName() + "\n");
|
//定位完成的时间
|
|
} else {
|
//定位失败
|
sb.append("定位失败" + "\n");
|
sb.append("错误码:" + location.getErrorCode() + "\n");
|
sb.append("错误信息:" + location.getErrorInfo() + "\n");
|
sb.append("错误描述:" + location.getLocationDetail() + "\n");
|
}
|
|
return sb.toString();
|
}
|
|
|
/**
|
* 经纬度转换具体位置
|
*/
|
private String transAddress(Double latitude, Double longitude) {
|
List<Address> addressList = null;
|
Geocoder geocoder = new Geocoder(mContext);
|
try {
|
addressList = geocoder.getFromLocation(latitude, longitude, 1);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
if (addressList != null && !addressList.isEmpty()) {
|
return addressList.get(0).getAddressLine(0);
|
}
|
return "未知位置";
|
}
|
|
|
@SuppressLint("MissingPermission")
|
public void getBestLocation(LocationGetListener listener) {
|
mListener = listener;
|
Criteria criteria = new Criteria();
|
Location location;
|
String provider = mLocationManager.getBestProvider(criteria, true);
|
if (TextUtils.isEmpty(provider)) {
|
location = getNetWorkLocation(mContext);
|
} else {
|
location = mLocationManager.getLastKnownLocation(provider);
|
}
|
if (location == null) {
|
provider = LocationManager.PASSIVE_PROVIDER;
|
location = mLocationManager.getLastKnownLocation(provider);
|
if (mListener != null) {
|
mListener.getLocationInfo(location);
|
}
|
|
mLocationManager.requestLocationUpdates(provider, 5 * 1000, 5, LocationProvider.this);
|
} else if (mListener != null) {
|
mListener.getLocationInfo(location);
|
}
|
}
|
|
@SuppressLint("MissingPermission")
|
public Location getNetWorkLocation(Context context) {
|
Location location = null;
|
|
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
|
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
}
|
return location;
|
}
|
|
public void unregisterLocationUpdaterListener() {
|
mLocationManager.removeUpdates(this);
|
}
|
|
@Override
|
public void onLocationChanged(Location location) {
|
if (mListener != null) {
|
mListener.getLocationInfo(location);
|
}
|
unregisterLocationUpdaterListener();
|
}
|
|
@Override
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
Log.v("location:", provider + " onStatusChanged. status = " + status);
|
}
|
|
@Override
|
public void onProviderEnabled(String provider) {
|
Log.v("location:", provider + " onProviderEnabled");
|
}
|
|
@Override
|
public void onProviderDisabled(String provider) {
|
Log.v("location:", provider + " onProviderDisabled");
|
|
}
|
}
|