package map.baidu.com;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.*;
import com.activity.chaoran.R;
import com.util.DialogUtil;

import java.util.HashMap;

public class BDMapActivity extends MapActivity {
	private MapView mMapView = null; // 地图View
	private MapController mMapController;
	private LBSLocationClientController mLocationClientController;
	private BMapManagerUtil app;
	private BDLocation bdLocation;

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bdmap);
		this.setTitle("正在定位......");
		app = (BMapManagerUtil) this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey,
					new BMapManagerUtil.MyGeneralListener());
		}
		super.initMapActivity(app.mBMapMan);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		mMapController = mMapView.getController();
		mMapController.setZoom(24);
		mLocationClientController = new LBSLocationClientController(this,
				new MyBDLocationListener());
		mLocationClientController.initializeLocationClientOption(); // 初始化定位选项（可调节）
		MyLocationOverlay myLocation = new MyLocationOverlay(this, mMapView);
		// 注册GPS位置更新的事件,让地图能实时显示当前位置
		myLocation.enableMyLocation();
		// 开启磁场感应传感器
		myLocation.enableCompass();
		mMapView.getOverlays().add(myLocation);
		ZoomControls zoomControls = (ZoomControls) mMapView.getChildAt(2);
		// mapView.removeViewAt(2);
		// 调整缩放控件的位置
		zoomControls.setPadding(0, 0, 0, 100);
		// 获取mapview中的百度地图图标
		ImageView iv = (ImageView) mMapView.getChildAt(1);
		// mapView.removeViewAt(1);
		// 调整百度地图图标的位置
		iv.setPadding(0, 0, 0, 100);
	}

	@Override
	protected void onPause() {
		mLocationClientController.stopLocationClient();
		app.mBMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mLocationClientController.startLocationClient();
		app.mBMapMan.start();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		app.onTerminate();
		bdLocation = null;
		app = null;
		mLocationClientController = null;
		mMapController = null;
		mMapView = null;
	}

	public void onSub(View v) {
		if (bdLocation != null) {
			HashMap map = new HashMap();
			map.put("latitude", bdLocation.getLatitude());
			map.put("longitude", bdLocation.getLongitude());
			map.put("gpsAddress", bdLocation.getAddrStr());
			Intent intent = getIntent();
			intent.putExtra("param", map);
			setResult(3, intent);
			finish();
		}else{
			DialogUtil.builder(BDMapActivity.this, "提示信息",
					"正在定位",0);
		}
	}

	public void onCurrentLocal(View v) {
		this.setTitle("正在定位......");
		mLocationClientController.requestLocationInformation();
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // TODO Auto-generated method stub
	// super.onCreateOptionsMenu(menu);
	// menu.add(0, 1, Menu.NONE, "放大");
	// menu.add(0, 2, Menu.NONE, "缩小");
	// return true;
	// }
	private class MyBDLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			Log.i("onReceiveLocation", "onReceiveLocation");
			if (location == null)
				return;
			bdLocation = location;
			mMapController.setCenter(new GeoPoint(
					(int) (location.getLatitude() * 1E6), (int) (location
							.getLongitude() * 1E6)));
			BDMapActivity.this.setTitle("定位成功 _" + location.getAddrStr());
		}

		@Override
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null)
				return;
		}
	}

}
