<?php if (!defined('THINK_PATH')) exit();?><script type="text/javascript">
var input_address_id="<?php echo ((isset($address_id) && ($address_id !== ""))?($address_id):'address'); ?>";
var jingDu ='<?php echo (isset($x)?$x:''); ?>' ;	//初始经度
var weiDu ='<?php echo (isset($y)?$y:''); ?>';		//初始纬度
var map_ak ='<?php echo (C("BAIDU_MAP_KEY")); ?>';//百度地图KEY
var input_x_id='<?php echo ((isset($x_name) && ($x_name !== ""))?($x_name):"x"); ?>';
var input_y_id='<?php echo ((isset($y_name) && ($y_name !== ""))?($y_name):"y"); ?>';
var city_name='<?php echo ((isset($city_name) && ($city_name !== ""))?($city_name):"石家庄"); ?>';
</script>
<style>
#map_wrap{width:938px;}
#allmap{width:938px;height:426px;overflow:hidden;zoom:1;position:relative;}
#result{width:100%;font-size:12px;height:10px;}
#xy{display:none;}
#info select{width:120px;margin-top:5px;margin-left:-5px;}
#suggestId{width:300px;}
#span_address{position:relative;left:-329px;}
#searchResultPanel{border:1px solid #C0C0C0;width:150px;height:auto;display:none;}
#jingWei{margin-top:4px}
#result img{cursor:pointer;position:relative;left:510px;top:14px;z-index:2;}
#map{height:100%;-webkit-transition:all 0.5s ease-in-out;transition:all 0.5s ease-in-out;}
.s_name{font-size:14px;}
dl,dt,dd,ul,li{margin:0;padding:0;list-style:none;}
p,span{font-size:12px;}
dt{font-size:12px;font-weight:bold;border-bottom:1px dotted #000;padding:5px 0 5px 5px;margin:5px 0;}
dd{padding:5px 0 0 5px;}
li{line-height:28px;}
.address{font-size:12px;}

.allxq{background-color: #fff;width:320px;max-height:320px;padding:10px; overflow-y:scroll;display:none;}
.allxq li{line-height: 20px;}
#all_dizhi{left:20px;position:absolute;z-index: 9999;}
.bmap-geolocation-con .bmap-geo{background:#fff url("https://webapi.amap.com/theme/v1.3/markers/b/loc_gray.png") no-repeat scroll 50% 50%;border:1px solid #ccc;border-radius:3px;height:35px;width:35px;right:4px}
</style>
<div id='map_wrap'>
<div id="all_dizhi">
	<div id="r-result" style="margin-top:4px;">
		<span class="address" style="" ></span>
        <input placeholder="请输入地址搜索" class="form-control f_l" style="display: inline;" type="text" id="suggestId" name="suggestId" size="20" value=""/>
        <input type="button" value="打点保存" class="confirm_map btn btn-primary"/>
		<div class="allxq" id="near">
			<ul id="pois">

			</ul>
		</div>
	</div>
</div>
<div class="bmap-controls"><div class="bmap-geolocation-con" style="position: absolute; z-index: 9999; right: 10px; bottom: 20px;"><div class="bmap-geo"></div></div></div>
<div id="allmap" ></div>
</div>
<input type="hidden" id="point_x" value=""/>
<input type="hidden" id="point_y" value=""/>
</body>
</html>
<script type="text/javascript">

// 百度地图API功能
var map = new BMap.Map("allmap");            // 创建Map实例
if(!jingDu || !weiDu){
    map.centerAndZoom(city_name, 13);//重新定位地图
}else{
    var point = new BMap.Point(jingDu,weiDu);    // 创建点坐标
    map.centerAndZoom(point,16);                     // 初始化地图,设置中心点坐标和地图级别。
    var marker = new BMap.Marker(point);  // 创建标注
    map.addOverlay(marker);               // 将标注添加到地图中
}
map.enableScrollWheelZoom();//启用滚轮放大缩小
//单击获取点击的经纬度
map.addEventListener("click",function(e){
  resetlocation(e.point.lng,e.point.lat);
  getpointinfo(e.point.lng,e.point.lat);
});


$(function(){
    $('#point_x').val(jingDu);
    $('#point_y').val(weiDu);
    $("#suggestId").focus(function(){$(this).val('');});
    $(".bmap-geolocation-con").click(function(){autolocation();});
    $("input[name='suggestId']").bind('keydown keyup',function(){
		get_detail_address();
	});
});
$('.confirm_map').click(function(){
    var jing=$('#point_x').val();
    var wei=$('#point_y').val();
    if(!jing || !wei){alert('请先选择坐标');return false;}
    if(input_x_id && input_y_id){
        $('#'+input_x_id).val(jing);
        $('#'+input_y_id).val(wei);
        $('.showlocation_'+input_x_id+input_y_id).text('已定位:'+jing+','+wei);
        $('.showlocation_'+input_x_id+input_y_id).attr('title','已定位:'+jing+','+wei);
    }
    if(input_address_id){
        var address=$('#suggestId').val();
        if(address)$('#'+input_address_id).val(address);
    }
    $.weeboxs.close("hanchao_url_box_map");

});
function resetlocation(x,y,name,is_center){
   if(!x || !y)return;
   map.clearOverlays();
   $('#point_x').val(x);
   $('#point_y').val(y);
   if(name)$("#suggestId").val(name);
   var point = new BMap.Point(x,y);
   var marker = new BMap.Marker(point);
  if(is_center)map.centerAndZoom(point,16);
   map.addOverlay(marker);
};
function get_detail_address(){
	var city_id=$("#city_id").val();
	var query=$("input[name='suggestId']").val();
	if(!city_name){$.showErr('请选择城市');return false;}
	if(!query)return false;
	var querydata=new Object();
		querydata.query=query;
		querydata.ak=map_ak;
		querydata.scope=1;
		querydata.output='json';
		querydata.page_size=10;
		querydata.region=encodeURIComponent(city_name);
	var url = '//api.map.baidu.com/place/v2/suggestion?'+ObjectToStr(querydata);
	$.ajax({
		url: url,
		type: "get",
		dataType: "jsonp",
		success: function (ret) {
			//alert(JSON.stringify(ret));
            if(ret.result&&ret.result.length>0){
                var html='';
                for(var i=0;i<ret.result.length;i++){
                    var lat = ret.result[i].location?ret.result[i].location.lat:0;
                    var lng = ret.result[i].location?ret.result[i].location.lng:0;
                    var name=ret.result[i].name;
                    var city=ret.result[i].city;
                    var district=ret.result[i].district;
					if(lat)html+='<li><a onclick="set_addr('+"'"+lng+"'"+','+"'"+lat+"'"+",'"+name+"'"+')"><h3>'+name+'</h3><p class="address">'+city+' '+district+'</p></a></li><div class="clear"></div>';
                }
                if(html){
                    $('.allxq').show();
                    $('#pois').html(html);
                }
            }
		}
	});
}
function getpointinfo(x,y){
    var myGeo = new BMap.Geocoder();
   var point = new BMap.Point(x,y);
	myGeo.getLocation(point, function(rs){
	   //alert(JSON.stringify(rs));
		var addComp = rs.addressComponents;//将点的市、区/县、街给addComp
		var s_province = addComp.province;//省市赋值给select
		var s_city = addComp.city;
		var s_district = addComp.district;
		if(addComp.province == addComp.city){
			var add_p_c =  addComp.city;
		}else{
			var add_p_c =  addComp.province+addComp.city;
		}
		var dizhi = add_p_c+addComp.district+addComp.street+addComp.streetNumber;
        if(rs['surroundingPois'][0]){
            dizhi+=rs['surroundingPois'][0]['title'];
        }
		//赋值
		$("#suggestId").val(dizhi);
	});
}
function autolocation(){
    var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
            //alert('您的位置：'+r.point.lng+','+r.point.lat);
	        resetlocation(r.point.lng,r.point.lat,'',1);
			map.panTo(r.point);
		}else {
			alert('failed'+this.getStatus());
		}
	},{enableHighAccuracy: true})
}
/*关闭选项*/
function clockAreas(obj) {
    $(obj).fadeOut();
    $(obj).prev().animate({"bottom": "-100%"});
}
function set_addr(x,y,name){
	resetlocation(x,y,name,1);
	var clos=$("#near");
	clockAreas(clos);
}
//将对象转化为&链接的字符串
function ObjectToStr(obj){
	if(typeof(obj)!='object'||!obj)return true;
	var newstr = '';
	for(var i in obj){
		newstr += '&'+i+'='+ obj[i];
	}
	return newstr;
}
</script>