//定位地图点 var baidumap=function (){ this.maps;//地图控件 this.map_show_id;//地图显示div的id this.map_search_button_id;//发址搜索按钮的classname this.map_search_value_id;//发址搜索值的classname this.map_field_id;//保存坐标的字段名称 this.showPoint;//如果修改信息的话需要显示之前的标记,这里请设置1 this.action_point_marker="";//每点击一次这里都会获取新的pint点 //this.default_poi;//增加信息的时候显示默认地图 //实例化地图 this.newmap=function(){ //查看地图字段是否有数据,如果有数据显示 var oldpoint=$("#"+this.map_field_id).val(); var point_l,point_n; if(oldpoint){ var str_arr=oldpoint.split(","); //字符分割 point_l=str_arr[0]; point_n=str_arr[1]; }else{ point_l=121.506309; point_n=31.245426; } this.maps = new BMap.Map(this.map_show_id, {enableMapClick:false});//构造底图时,关闭底图可点功能 var poi = new BMap.Point(point_l,point_n); this.maps.centerAndZoom(poi, 16); this.maps.enableScrollWheelZoom(); this.maps.setDefaultCursor("crosshair"); //查看地图字段是否有数据,如果有数据显示 if(oldpoint){ this.map_position_doing(poi); } } // 创建地址解析器实例 this.myGeo = new BMap.Geocoder(); //地址模糊搜索 this.map_search=function(){ var newobj=this;//注意这里 因为下面的函数是点击后再产生的,直接用this是获取函数本身的而不是类的this,所以要重新赋值一下 $("#"+this.map_search_button_id).click(function(){ var address=$("#"+newobj.map_search_value_id).val(); if(!address){ layer.alert("请输入地址"); return ; } newobj.myGeo.getPoint(address, function(point){ if (point) { newobj.map_position_doing(point,1); }else{ layer.alert("您输入的地址没有搜索到结果,请输入具体的地址信息!"); } }, "上海市"); }); } //鼠标点击拾取坐标 this.map_position=function(){ var newobj=this;//注意这里 因为下面的函数是点击后再产生的,直接用this是获取函数本身的而不是类的this,所以要重新赋值一下 this.maps.addEventListener("click",function(e){ newobj.map_position_doing(e.point); }); } //根据提交过来的坐标标记 this.map_position_doing=function(point,setpos){ var myIcon = new BMap.Icon("/Public/baidumap/img/pos.png", new BMap.Size(32,32)); //清除老的标志 if(this.action_point_marker){ this.maps.removeOverlay(this.action_point_marker); } this.action_point_marker= new BMap.Marker(point,{icon:myIcon}); // 创建标注 //这里主要用于模糊搜索的时候将视图切换到点上 if(setpos){ this.maps.centerAndZoom(point,18); } this.maps.addOverlay(this.action_point_marker); //放到input框里 $("#"+this.map_field_id).val(point.lng+","+point.lat); } }