package com.imofan.android.basic;

import android.content.Context;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * 1.在 app下添加百度定位包locSDK_xx.0.jar 和 liblocSDK4.so 文件 
 * 
 * 定位
 * @author liyang
 *
 */
public class LocationService {
	private final String TAG = "PcGroupLocationService";
	private LocationClient mLocationClient;
	private static String mapKey; 
	
	/**
	 * 在app application类中初始化，需要在魔方onResume执行之前初始化
	 * @param accessKey
	 */
	public static void initAK(String accessKey){
		mapKey=accessKey;
	}
	
	/**
	 * 开始定位,不设置option.setScanSpan()时只定位一次，不更新，该方法必须在主线程中调用。
	 * 因为LocationClient类必须在主线程中声明。需要Context类型的参数。
	 * Context需要时全进程有效的context,推荐用getApplicationConext获取全进程有效的context
	 * @param context
	 */
	public static void startLocation(final Context context, final LocationSuccessListener successListener){
		if(null != context){
			LocationService service = new LocationService();
			service.init(context, successListener);
		}
	}
	
	private void init(final Context context, final LocationSuccessListener successListener) {
		mLocationClient = new LocationClient(context);
		//汽车网申请的秘钥
		mLocationClient.setAK(mapKey);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");   //返回的定位结果包含地址信息
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
//		option.disableCache(true);   //禁止启用缓存定位,默认开启缓存定位
//		option.setScanSpan(5000);    //设置发起定位请求的间隔时间为5000ms,不设置则默认定位一次成功后不再次请求
		mLocationClient.setLocOption(option);
		mLocationClient.registerLocationListener(new BDLocationListener() {

			/**
			 * 接收异步返回的定位结果
			 */
			@Override
			public void onReceiveLocation(BDLocation location) {
				
				if (location == null) {return;}
				LocationResult result = new LocationResult();
				result.setProvince(location.getProvince())
					  .setCity(location.getCity())
					  .setDistrict(location.getDistrict())
					  .setAddress(location.getAddrStr())
					  .setStreet(location.getStreet())
					  .setStreetNum(location.getStreetNumber())
					  .setTime(location.getTime())
					  .setCityCode(location.getCityCode())
					  .setLatitude(location.getLatitude())
					  .setLngitude(location.getLongitude());
				if(null != successListener){successListener.onLocationResult(result);}
                if(null != mLocationClient){mLocationClient.stop();}
			}

			/**
			 * 接收异步返回的POI查询结果
			 */
			@Override
			public void onReceivePoi(BDLocation poiLocation) {}
			
		});
		if (null != mLocationClient && !mLocationClient.isStarted()) {
			mLocationClient.requestLocation();
			mLocationClient.start();
		} else {
			Log.v(TAG, "定位失败！");
		}

	}

	/**
	 * 定位成功监听器
	 * @author xjzhao
	 *
	 */
	public interface LocationSuccessListener{
		public void onLocationResult(LocationResult result);
	}
	
	/**
	 * 包装定位结果数据
	 * @author xjzhao
	 *
	 */
	public class LocationResult{
		private String province;   //省份
		private String city;       //城市
		private String district;   //区或者县
		private String address;    //地址
		private String street;     //街道
		private String streetNum;  //门牌号
		private String time;       //时间
		private String cityCode;   //城市编码
		private double latitude;   //纬度
		private double lngitude;   //经度
		public String getProvince() {
			return province;
		}
		public LocationResult setProvince(String province) {
			this.province = province;
			return this;
		}
		public String getCity() {
			return city;
		}
		public LocationResult setCity(String city) {
			this.city = city;
			return this;
		}
		public String getDistrict() {
			return district;
		}
		public LocationResult setDistrict(String district) {
			this.district = district;
			return this;
		}
		public String getAddress() {
			return address;
		}
		public LocationResult setAddress(String address) {
			this.address = address;
			return this;
		}
		public String getStreet() {
			return street;
		}
		public LocationResult setStreet(String street) {
			this.street = street;
			return this;
		}
		public String getStreetNum() {
			return streetNum;
		}
		public LocationResult setStreetNum(String streetNum) {
			this.streetNum = streetNum;
			return this;
		}
		public String getTime() {
			return time;
		}
		public LocationResult setTime(String time) {
			this.time = time;
			return this;
		}
		public String getCityCode() {
			return cityCode;
		}
		public LocationResult setCityCode(String cityCode) {
			this.cityCode = cityCode;
			return this;
		}
		public double getLatitude() {
			return latitude;
		}
		public LocationResult setLatitude(double latitude) {
			this.latitude = latitude;
			return this;
		}
		public double getLngitude() {
			return lngitude;
		}
		public LocationResult setLngitude(double lngitude) {
			this.lngitude = lngitude;
			return this;
		}
	}
}
