<?php
	if ($city == '') {return false;}
	switch ($city)
	{
		case 'hf':
			$lng = 117.282699;
			$lat = 31.866942;
			break;
		case 'sz':
			$lng = 120.602085;
			$lat = 31.303672;
			break;
		case 'wx':
			$lng = 120.31293;
			$lat = 31.563145;
			break;
		case 'xa':
			$lng = 108.953098;
			$lat = 34.2778;
			break;
		case 'cz':
			$lng = 119.981861;
			$lat = 31.813628;
			break;
		case 'cq':
			$lng = 106.555931;
			$lat = 29.566222;
			break;
		case 'wh':
			$lng = 118.381233;
			$lat = 31.329506;
			break;
		case 'hz':
			$lng = 120.165333;
			$lat = 30.269226;
			break;
		case 'ks':
			$lng = 120.98593;
			$lat = 31.384196;
			break;
		case 'sy':
			$lng = 123.438975;
			$lat = 41.81134;
			break;
        case 'nj';
            $lng = 118.78333;
            $lat = 32.05000;
		//其它城市扩展
		default:
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
	#allmap {width: 1000px; height: 600px; overflow: hidden;margin:0;}
	#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
	#r-result{height:100%;width:20%;float:left;}
</style>
<script src="http://www.house365.com/public/js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
<title>365地产家居网</title>
</head>
<body>
	<div>
		<p>
			<input type="text" size="60" name="address" value="<?php echo $_GET['address']; ?>" id="address">
			<input type="button" value="查找坐标" id="searchLocal">
		</p>
		<div id="allmap"></div>
		<p>
			地图坐标map_x：<input type="text" size="50" id="map_x"><br>
			地图坐标map_y：<input type="text" size="50" id="map_y">
		</p>
	</div>

	<script type="text/javascript">
		var lng = "<?=$lng?>";
		var lat = "<?=$lat?>";
		function buildMap(arrMapParameter)
		{
			var map = new BMap.Map("allmap");                // 创建Map实例
			var point = new BMap.Point(arrMapParameter.lng, arrMapParameter.lat);     // 创建点坐标
			map.centerAndZoom(point, arrMapParameter.zoom);                     // 初始化地图,设置中心点坐标和地图级别。
			map.enableScrollWheelZoom();                     //启用滚轮放大缩小
			map.addControl(new BMap.NavigationControl());    
			map.addControl(new BMap.ScaleControl()); 
			map.addControl(new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_RIGHT}))
			map.addControl(new BMap.OverviewMapControl());              //添加默认缩略地图控件
			map.addControl(new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT}));   //右上角，打开
			return map;
		}

		function getLocalPoint(strAddress)
		{
			//自定义搜索回调数据  
			var options = {
				selectFirstResult : true,
				autoViewport : true,
				onSearchComplete: function(results) {    
					if (local.getStatus() == BMAP_STATUS_SUCCESS) {    
						var b = local._json.content.level || 17;

						 //objMap.clearOverlays();
						 //强制取其中一个点
						 var objMarkFirst = results.getPoi(0);
						
						 var objMap = buildMap({'lng' : objMarkFirst.point.lng, 'lat' : objMarkFirst.point.lat, 'zoom' : b});
						 //console.log(objMarkFirst);
						 // 创建标注
						 var marker = new BMap.Marker(
							 new BMap.Point(objMarkFirst.point.lng, objMarkFirst.point.lat), 
							 {enableDragging: true}
						 ); 
						 objMap.addOverlay(marker);
						 setMapPoint({'lng' : objMarkFirst.point.lng, 'lat' : objMarkFirst.point.lat});
						 //marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

						 //marker.enableDragging = true; 
						 marker.addEventListener('dragend', function(e) {
							setMapPoint(e.point);
						 });
					}     
				}
			}; 
			var local = new BMap.LocalSearch(objMap, options);  
			local.search(strAddress);
		}
	
		function setMapPoint(arrPoint)
		{
			 document.domain = 'house365.com';
			 $('#map_x').val(arrPoint.lng);
			 $('#map_y').val(arrPoint.lat);
			 if (window.opener != null)
			 {
				window.opener.document.getElementById("b_map_x").value = arrPoint.lng;
				window.opener.document.getElementById("b_map_y").value = arrPoint.lat;
			 }

		}

		function setSearchAddress()
		{
			var strAddress = $('#address').val();
			var strPattern = /^\s$/;
			if (strAddress == '' || strPattern.test(strAddress))
			{
				alert('地址不能为空，请重新输入');
				$('#address').focus();
				return false;
			}
			getLocalPoint(strAddress);
		}

		$('document').ready(function(){
			//生成地图
			window.objMap = buildMap({'lng' : lng, 'lat' : lat, 'zoom' : 12});
			$('#searchLocal').bind('click', function() {
				setSearchAddress();
			});
		});



		$('#address').bind('keydown', function(evt) {
			if (evt.keyCode == 71)
			{
				setSearchAddress();
			}
		});
	</script>
</body>
</html>

