package map.baidu.com;

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;

public class LBSLocationClientController {

	private LocationClient mLocationClient;

	public LBSLocationClientController(Context context,
			BDLocationListener listener) {
		initializeLocationClient(context, listener);
	}

	private void initializeLocationClient(Context context,
			BDLocationListener listener) {

		mLocationClient = new LocationClient(context);
		mLocationClient.registerLocationListener(listener);
	}

	private boolean isLocationClientPrepared() {
		return mLocationClient != null && mLocationClient.isStarted();
	}

	public void setLocationOption(LocationClientOption option) {
		mLocationClient.setLocOption(option);
	}

	public void startLocationClient() {
		if (mLocationClient != null) {
			System.out.println("------------------------------------------启动");
			mLocationClient.start();
		}
	}

	public void stopLocationClient() {
		if (mLocationClient != null) {
			mLocationClient.stop();
		}
	}

	public void requestPIOInformation() {
		if (isLocationClientPrepared()) {
			Log.i("requestPoi", "requestPoi");
			mLocationClient.requestPoi();
		}
	}

	public void requestLocationInformation() {
		if (isLocationClientPrepared()) {
			Log.i("requestLocation", "requestLocation");
			mLocationClient.requestLocation();
		}
	}

	public StringBuffer buildLocationStringBuffer(BDLocation location) {
		StringBuffer sb = new StringBuffer(256);
		sb.append("time : ");
		sb.append(location.getTime());
		sb.append("\nerror code : ");
		sb.append(location.getLocType());
		sb.append("\nlatitude : ");
		sb.append(location.getLatitude());
		sb.append("\nlontitude : ");
		sb.append(location.getLongitude());
		sb.append("\nradius : ");
		sb.append(location.getRadius());
		if (location.getLocType() == BDLocation.TypeGpsLocation) {
			sb.append("\nspeed : ");
			sb.append(location.getSpeed());
			sb.append("\nsatellite : ");
			sb.append(location.getSatelliteNumber());
		} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
			sb.append("\naddr : ");
			sb.append(location.getAddrStr());
		}
		return sb;
	}

	public StringBuffer buildPIOStringBuffer(BDLocation poiLocation) {
		StringBuffer sb = new StringBuffer(256);
		sb.append("Poi time : ");
		sb.append(poiLocation.getTime());
		sb.append("\nerror code : ");
		sb.append(poiLocation.getLocType());
		sb.append("\nlatitude : ");
		sb.append(poiLocation.getLatitude());
		sb.append("\nlontitude : ");
		sb.append(poiLocation.getLongitude());
		sb.append("\nradius : ");
		sb.append(poiLocation.getRadius());
		if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
			sb.append("\naddr : ");
			sb.append(poiLocation.getAddrStr());
		}
		if (poiLocation.hasPoi()) {
			sb.append("\nPoi:");
			sb.append(poiLocation.getPoi());
		} else {
			sb.append("noPoi information");
		}
		return sb;
	}

	public void initializeLocationClientOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 开启GPS
//		option.setAddrType("detail");
		option.setCoorType("bd09ll");// gcj02
//		option.setScanSpan(800);
		option.setServiceName("crtech");	
		option.setProdName("crtech");      
		option.setAddrType("all");
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(500); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		setLocationOption(option);
	}

}
