map - 副本.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //地图盒子的高度
  2. var windheight=$(window).height();
  3. var divheight=$('.y_maphead').height();
  4. var navheight=$('.y_puicdh_c').height();
  5. var windwidth=$(window).width();
  6. var divwidth=$('.y_mapcenter_left').width();
  7. $('.y_mapcenter').css({'height':(windheight-divheight-navheight)+'px'});
  8. $('.y_mapcenter_main').css({'width':(windwidth-divwidth-20)+'px'});
  9. // $('.y_mapcond_main .y_mapcond_right span a').hide();
  10. // 地图找房 区域效果
  11. $(function(){
  12. $('.y_maptop_scr').keyup(function(event) {
  13. if(event.keyCode==13){
  14. $('.y_maptop_sub').click();
  15. console.log('提交');
  16. return false;
  17. }else{
  18. lplist();
  19. }
  20. });
  21. navMouseleave('#y_maptop_sel1','#y_maptop_sel1 .y_qysf');
  22. navMouseleave('#y_maptop_sel2','#y_maptop_sel2 .y_qysf');
  23. navMouseleave('#y_maptop_sel3','#y_maptop_sel3 .y_qysf');
  24. navMouseleave('#y_maptop_sel4','#y_maptop_sel4 .y_qysf');
  25. $('#y_maptop_sel1 .y_left a').on('mouseover',function(){
  26. var cyit=$(this).attr('city');
  27. var sjtitle=$(this).text();
  28. $(this).css({'color':'#f00'}).siblings().css({'color':'#666'});
  29. $('#y_maptop_sel1 p.y_right').attr('sj-city',''+sjtitle+'')
  30. $.post('/js/navs.json',{cyit:cyit,csrf_token_f:111},function(data){
  31. var echodataUl = $('#y_maptop_sel1 p.y_right');
  32. echodataUl.html('');
  33. var data = data.data;
  34. var html = '';
  35. $.each(data,function(key,val){
  36. html ='<a data-city="'+val.id+'" href="javascript:;">';
  37. html+=''+val.title+'';
  38. html+='</a>';
  39. echodataUl.append(html);
  40. })
  41. })
  42. });
  43. $('#y_maptop_sel1 p.y_right').on('click','a',function(){
  44. var title=$('#y_maptop_sel1 p.y_right').attr('sj-city');
  45. var datacity=$(this).attr('data-city');
  46. var name_title=$(this).text();
  47. $('#y_maptop_sel1 p.y_city span').text(title);
  48. $('.y_mapcond_right span.quyu em').text(name_title);
  49. $('.y_mapcond_right span.quyu a').show();
  50. lplist();
  51. });
  52. y_leixing('y_maptop_sel2','a','yusuan');
  53. y_leixing('y_maptop_sel3','a','huxing');
  54. y_leixing('y_maptop_sel4','a','tese');
  55. fontext('quyu','#y_maptop_sel1 p.y_city span','区域');
  56. fontext('yusuan','#y_maptop_sel2 p.y_city span','预算');
  57. fontext('huxing','#y_maptop_sel3 p.y_city span','户型');
  58. fontext('tese','#y_maptop_sel4 p.y_city span','特色');
  59. $('.y_mapcond_left a').on('click',function(){
  60. $('.y_mapcond_right span em').html('');
  61. $('.y_mapcond_right span a').hide();
  62. $('#y_maptop_sel1 p.y_city span').text('区域');
  63. $('#y_maptop_sel2 p.y_city span').text('预算');
  64. $('#y_maptop_sel3 p.y_city span').text('户型');
  65. $('#y_maptop_sel4 p.y_city span').text('特色');
  66. lplist();
  67. })
  68. })
  69. function fontext(name1,name2,text){
  70. $('.y_mapcond_right').on('click','span.'+name1+' a',function(){
  71. $(this).siblings('em').html('');
  72. $(this).hide();
  73. $(name2).text(text);
  74. lplist();
  75. })
  76. }
  77. function y_leixing(name1,name2,name3){
  78. $('#'+name1+' p.y_leixing').on('click',name2,function(){
  79. var title=$(this).text();
  80. var name_title=$(this).text();
  81. var name_title=$(this).text();
  82. $('#'+name1+' p.y_city span').text(title);
  83. $('.y_mapcond_right span.'+name3+' em').text(name_title);
  84. $('.y_mapcond_right span.'+name3+' a').show();
  85. lplist();
  86. });
  87. }
  88. function navMouseleave (id,id1){
  89. $(id).on({
  90. mouseover: function() {
  91. $(id1).show();
  92. },
  93. mouseleave: function() {
  94. $(id1).hide();
  95. }
  96. });
  97. }
  98. $('#y_maptop_sel1 .y_left a').on('click',function(){
  99. $('#y_maptop_sel1 p.y_city span').text($(this).text());
  100. var post=$(this).attr('post')
  101. var _pointx = post.split(',')[0];
  102. var _pointy = post.split(',')[1];
  103. point=new BMap.Point(_pointx,_pointy);
  104. mp.centerAndZoom(point, 8);
  105. mp.enableScrollWheelZoom();
  106. lplist();
  107. })
  108. var mp = new BMap.Map("allmap",{maxZoom:16});
  109. mp.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
  110. // mp.addControl(new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT})); //右下角,打开
  111. mp.addControl(new BMap.ScaleControl()); // 添加默认比例尺控件
  112. mp.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT})); // 左下
  113. mp.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
  114. mp.addControl(new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_SMALL})); //右上角,仅包含平移和缩放按钮
  115. point=new BMap.Point(110.483789,19.119914);
  116. mp.centerAndZoom(point, 8);
  117. mp.enableScrollWheelZoom();
  118. var geoc = new BMap.Geocoder();
  119. // console.log(geoc);
  120. // 复杂的自定义覆盖物
  121. function ComplexCustomOverlay(point, text, mouseoverText){
  122. this._point = point;
  123. this._text = text;
  124. this._overText = mouseoverText;
  125. }
  126. ComplexCustomOverlay.prototype = new BMap.Overlay();
  127. ComplexCustomOverlay.prototype.initialize = function(map){
  128. this._map = map;
  129. var div = this._div = document.createElement("div");
  130. div.style.position = "absolute";
  131. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  132. var span = this._span = document.createElement("span");
  133. var that = this;
  134. var arrow = this._arrow = document.createElement("div");
  135. arrow.style.background = "url(../map/image/ico_map9.png) no-repeat";
  136. arrow.style.position = "absolute";
  137. arrow.style.width = "24px";
  138. arrow.style.height = "28px";
  139. arrow.style.top = "20px";
  140. arrow.style.left = "10px";
  141. arrow.style.overflow = "hidden";
  142. div.appendChild(arrow);
  143. div.onmouseover = function(){
  144. this.style.background = "url(../map/image/ico_map9.png) no-repeat";
  145. arrow.style.backgroundPosition = "0px -35px";
  146. this.style.zIndex = 999999;
  147. // this.style.cursor="pointer";
  148. }
  149. div.onmouseout = function(){
  150. this.style.background = "url(../map/image/ico_map9.png) no-repeat";
  151. arrow.style.backgroundPosition = "0px 0px";
  152. this.style.zIndex = 0;
  153. }
  154. mp.getPanes().labelPane.appendChild(div);
  155. return div;
  156. }
  157. ComplexCustomOverlay.prototype.draw = function(){
  158. var map = this._map;
  159. var pixel = map.pointToOverlayPixel(this._point);
  160. this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
  161. this._div.style.top = pixel.y - 35+ "px";
  162. };
  163. ComplexCustomOverlay.prototype.addEventListener = function(event,fun){
  164. this._div['on'+event] = fun;
  165. };
  166. lplist();
  167. function lplist(){
  168. $(".y_mapcenter_list ul").html('')
  169. var html = '';
  170. $.ajax({
  171. url: "/map/js/tsconfig.json",
  172. type: "GET",
  173. dataType: "json",
  174. success: function(data) {//请求成功完成后要执行的方法
  175. $.each(data.markerArr, function (i, data) { //左边楼盘列表
  176. var jg=data.content.pic;
  177. if (jg!='') {
  178. jg=data.content.pic;
  179. }else{
  180. jg='待定';
  181. };
  182. html+= '<li class="c" data-id="'+data.point+'">';
  183. html+= '<div class="y_tu"><a href="javascript:;"><img src="'+data.content.img+'" alt=""></a></div>';
  184. html+= '<div class="y_textmain">';
  185. html+= '<div class="y_text c">';
  186. html+= '<a href="javascript:;">'+data.title+'</a>';
  187. html+= '<span><em>'+jg+'</em></span>';
  188. html+= '</div>';
  189. html+= '<div class="y_qixian">(有效期:2017年2月11日-2018年2月11日)</div>';
  190. html+= '<div class="y_hx">户型:'+data.content.add+'</div>';
  191. html+= '<div class="y_tese c">';
  192. html+= '<i>旅游地产</i><i>旅游地产</i><i>旅游地产</i>';
  193. html+= '</div>';
  194. html+= '</div> ';
  195. html+= '</li>';
  196. });
  197. $(".y_mapcenter_list ul").append(html);
  198. function createInfoWindow(data){ //自定义覆盖物
  199. var iw= new BMap.InfoWindow(
  200. '<div class="map_id" style="width:260px; height:auto; position:absolute; left:0px; top:0px;">'+
  201. '<p style="width:100%; height:155px; margin-top:10px;"><a href="'+data.content.href+'"><img src="'+data.content.img+'" alt="" width="100%" height="100%;"></a></p>'+
  202. '<p style="margin-top:8px;"><a style="font-size:16px; color:#333;" href="'+data.content.href+'">'+data.title+'</a><span style="font-size:12px; color:#888; padding-left:8px;">'+data.dq+'</span></p>'+
  203. '<p style="margin-top:5px;"><span><em style="font-size:18px; color:#f00; font-style:normal;">'+data.content.pic+'</em></span></p>'+
  204. '</div>'
  205. );
  206. return iw
  207. }
  208. // 遍历覆盖楼盘名称
  209. function labal(obj){
  210. $.each(obj.markerArr, function (i, obj) {
  211. var txt = obj.title;
  212. var mouseoverTxt = txt + '&nbsp;&nbsp;|&nbsp;&nbsp;' + obj.content.pic;
  213. var pintx = obj.point.split('|')[0];
  214. var pinty = obj.point.split('|')[1];
  215. var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(pintx, pinty), txt, mouseoverTxt); //将覆盖物存到变量,以便下面使用。
  216. mp.addOverlay(myCompOverlay);
  217. var point = new BMap.Point(pintx, pinty);
  218. var _iw = createInfoWindow(obj);
  219. var _marker = myCompOverlay; //当初存的覆盖物变量
  220. _marker.addEventListener("click", function () { //点击出现对应自定义覆盖物
  221. mp.openInfoWindow(_iw, point);
  222. });
  223. });
  224. }
  225. $(".y_mapcenter_list ul li").click(function(){ //点击列表对应自定义覆盖物
  226. var _point = $(this).attr('data-id');
  227. var _pointx = _point.split('|')[0];
  228. var _pointy = _point.split('|')[1];
  229. var _points = new BMap.Point(_pointx, _pointy);
  230. var _i = $(this).index();
  231. var _newhou = data.markerArr[_i];
  232. var ih = createInfoWindow(_newhou);
  233. mp.centerAndZoom(_points, 14); //对应的坐标及放大级别
  234. labal(data); //遍历所以有的点
  235. mp.openInfoWindow(ih, _points) //当前覆盖添加并展示
  236. });
  237. // 方法2:用于监听滚轮事件
  238. //firefox使用DOMMouseScroll,其他浏览器使用mousewheel
  239. $(document).bind('mousewheel DOMMouseScroll',fullscreenScroll);
  240. function fullscreenScroll(e){
  241. var delta = -e.originalEvent.wheelDelta || e.originalEvent.detail;//firefox使用detail:下3上-3,其他浏览器使用wheelDelta:下-120上120//下滚
  242. if(delta>0){
  243. e=e || window.event;
  244. var u = mp.getZoom(); // 定义地图缩放等级的变量
  245. if(u > 14){
  246. // console.log(u)
  247. labal(data); //遍历所有的点
  248. }else if(u >13){
  249. mp.clearOverlays(); //清除地图上所有的覆盖物
  250. // console.log(u)
  251. }
  252. }
  253. //上滚
  254. if(delta<0){
  255. e=e || window.event;
  256. var u = mp.getZoom(); // 定义地图缩放等级的变量
  257. if(u >= 13){
  258. // console.log(u)
  259. labal(data); //遍历所有的点
  260. }else{
  261. mp.clearOverlays(); //清除地图上所有的覆盖物
  262. }
  263. // console.log(u + 'a')
  264. }
  265. }
  266. mp.addEventListener("click",attribute); //获取点击聚合图标放大的缩放级别 2018.3.21
  267. function attribute(){
  268. var u = mp.getZoom(); // 定义地图缩放等级的变量
  269. if(u >= 13){
  270. labal(data);
  271. }
  272. }
  273. // ----------------------------------- 不调用 maplib.js textlconOverlay.js 不使用下面的代码 ------------------------------------------------------
  274. var markers = [];
  275. var _conter = [];
  276. var _data = [];
  277. var pt = null;
  278. var msc;
  279. // console.log(data.markerArr.length)
  280. for(var i in data.markerArr){
  281. var coordinate = newPoint(data.markerArr[i].point);
  282. pt = new BMap.Point(coordinate[0],coordinate[1]);
  283. markers.push(new BMap.Marker(pt));
  284. _conter.push(data.markerArr[i].title);
  285. _data.push({"point":pt,"title":data.markerArr[i].title});
  286. //最简单的用法,生成一个marker数组,然后调用markerClusterer类即可。
  287. var markerClusterer = new BMapLib.MarkerClusterer(mp,
  288. {
  289. markers:markers,
  290. girdSize : 100,
  291. getCent:data,
  292. ladata:_data,
  293. geoc:geoc,
  294. // minClusterSize:3,
  295. styles : [{
  296. url:'/map/image/blue.png',
  297. size: new BMap.Size(92, 92),
  298. backgroundColor : '#2E96F7'
  299. }],
  300. });
  301. markerClusterer.setMaxZoom(13);
  302. markerClusterer.setGridSize(100);
  303. }
  304. }
  305. });
  306. }
  307. // 处理经纬坐标
  308. function newPoint(_point){
  309. var pointArr = [];
  310. var _pointx = _point.split('|')[0];
  311. var _pointy = _point.split('|')[1];
  312. pointArr.push(_pointx,_pointy);
  313. return pointArr;
  314. }