package com.chj.baidumap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.chj.baidumap.base.BaseActivity;

/**
 * 
 * @°üÃû:com.chj.baidumap
 * @ÀàÃû:PoiSearchNearByDemo
 * @×÷Õß:³Â»ð¾æ
 * @Ê±¼ä:2015-8-18 ÏÂÎç5:09:39
 * 
 * @ÃèÊö:ÖÜ±ßËÑË÷,¼´Ô²ÐÎÇøÓòËÑË÷
 * 
 * @SVN°æ±¾ºÅ:$Rev$
 * @¸üÐÂÈË:$Author$
 * @¸üÐÂÃèÊö:TODO
 * 
 */
public class PoiSearchNearByDemo extends BaseActivity
{
	private static String	KEY_WORD	= "À¿ÔÂºþ";
	private PoiSearch		poiSearch;				// POI¼ìË÷½Ó¿Ú¶ÔÏó

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		poiSearch = PoiSearch.newInstance();
		poiSearch.setOnGetPoiSearchResultListener(new MyListener());

		search();
	}

	/**
	 * ËÑË÷
	 * 
	 * @¹ÄÀËÓì:24.452261,118.073486
	 */
	private void search()
	{
		// Ô²ÐÎ¸²¸ÇÇøÓò
		CircleOptions circleOptions = new CircleOptions();
		circleOptions.center(new LatLng(33.640877, 114.690423)).radius(1000).fillColor(0x60FF0000);
		baiduMap.addOverlay(circleOptions);

		// ÖÜ±ßËÑË÷
		PoiNearbySearchOption nearbyOption = new PoiNearbySearchOption();
		nearbyOption.location(new LatLng(33.640877, 114.690423));// ÉèÖÃÖÐÐÄµã
		nearbyOption.radius(1000);// ÉèÖÃ°ë¾¶£¬µ¥Î»ÊÇÃ×
		nearbyOption.keyword(KEY_WORD);// ÉèÖÃ¹Ø¼ü×Ö
		poiSearch.searchNearby(nearbyOption);
	}

	/**
	 * ×Ô¶¨ÒåÀà
	 */
	class MyListener implements OnGetPoiSearchResultListener
	{

		@Override
		public void onGetPoiDetailResult(PoiDetailResult result)
		{
			if (result == null || SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error)
			{
				Toast.makeText(getApplicationContext(), "Î´ËÑË÷µ½½á¹û", 0).show();
				return;
			}

			// String text = result.getAddress() + "::" + result.getCommentNum()
			// + "::" + result.getEnvironmentRating();
			// Toast.makeText(getApplicationContext(), text, 0).show();
		}

		@Override
		public void onGetPoiResult(PoiResult result)
		{
			if (result == null || SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error)
			{
				Toast.makeText(getApplicationContext(), "Î´ËÑË÷µ½½á¹û", 0).show();
				return;
			}
			PoiOverlay overlay = new MyPoiOverlay(baiduMap);// ´¦ÀípoiµÄ¸²¸ÇÎï
			baiduMap.setOnMarkerClickListener(overlay);// °ÑÊÂ¼þ·Ö·¢¸øoverlay,overlay²ÅÄÜ´¦Àíµã»÷ÊÂ¼þ
			overlay.setData(result);// ÉèÖÃ½á¹û
			overlay.addToMap();// ½«ËÑË÷ËùÓÐOverlay Ìí¼Óµ½µØÍ¼ÉÏ
			overlay.zoomToSpan();// Ëõ·ÅµØÍ¼£¬Ê¹ËùÓÐOverlay¶¼ÔÚºÏÊÊµÄÊÓÒ°ÄÚ
									// ×¢:¸Ã·½·¨Ö»¶ÔMarkerÀàÐÍµÄoverlayÓÐÐ§
		}

	}

	class MyPoiOverlay extends PoiOverlay
	{

		public MyPoiOverlay(BaiduMap arg0) {
			super(arg0);
		}

		@Override
		public boolean onPoiClick(int position)
		{
			PoiResult poiResult = getPoiResult();
			PoiInfo poiInfo = poiResult.getAllPoi().get(position);// µÃµ½µã»÷µÄÄÇ¸öpoiÐÅÏ¢
			String text = poiInfo.name + "," + poiInfo.address;
			Toast.makeText(getApplicationContext(), text, 1).show();

			PoiDetailSearchOption detailOption = new PoiDetailSearchOption();
			detailOption.poiUid(poiInfo.uid);// ÉèÖÃpoiµÄuid,Óû¼ìË÷µÄpoiµÄuid
			poiSearch.searchPoiDetail(detailOption);// POI ÏêÇé¼ìË÷
			return super.onPoiClick(position);
		}
	}

	/**
	 * °´Å¥µã»÷ÊÂ¼þ
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		switch (keyCode)
		{
			case KeyEvent.KEYCODE_MENU:
				AlertDialog.Builder builder = new AlertDialog.Builder(PoiSearchNearByDemo.this);
				builder.setTitle("ËÑË÷");
				View view = View.inflate(getApplicationContext(), R.layout.poi_search_items, null);
				final EditText et_key = (EditText) view.findViewById(R.id.poi_et_key_items);
				builder.setView(view);
				builder.setPositiveButton("È·¶¨", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						baiduMap.clear();// Çå¿ÕµØÍ¼ËùÓÐµÄ Overlay ¸²¸ÇÎïÒÔ¼° InfoWindow
						KEY_WORD = et_key.getText().toString();
						search();
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("È¡Ïû", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						dialog.dismiss();
					}
				});
				builder.show();
				break;
			default:
				break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
