map - 副本2018.7.4完数据加载.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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",{minZoom:5,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. point=new BMap.Point(106.367394,34.111821);
  117. mp.centerAndZoom(point, 5);
  118. mp.enableScrollWheelZoom();
  119. var geoc = new BMap.Geocoder();
  120. // console.log(geoc);
  121. // 复杂的自定义覆盖物
  122. function ComplexCustomOverlays(point, text, mouseoverText){
  123. this._point = point;
  124. this._text = text;
  125. this._overText = mouseoverText;
  126. }
  127. ComplexCustomOverlays.prototype = new BMap.Overlay();
  128. ComplexCustomOverlays.prototype.initialize = function(map){
  129. this._map = map;
  130. var div = this._div = document.createElement("div");
  131. div.style.position = "absolute";
  132. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  133. var span = this._span = document.createElement("span");
  134. var that = this;
  135. var arrows = this._arrows = document.createElement("div");
  136. arrows.style.background = "url(../map/image/ico_map9.png) no-repeat";
  137. arrows.style.position = "absolute";
  138. arrows.style.width = "24px";
  139. arrows.style.height = "28px";
  140. arrows.style.top = "10px";
  141. arrows.style.left = "0px";
  142. arrows.style.overflow = "hidden";
  143. arrows.style.cursor = "default";
  144. div.appendChild(arrows);
  145. div.onmouseover = function(){
  146. this.style.background = "url(../map/image/ico_map9.png) no-repeat";
  147. arrows.style.backgroundPosition = "0px -35px";
  148. this.style.zIndex = 999999;
  149. // this.style.cursor="pointer";
  150. }
  151. div.onmouseout = function(){
  152. this.style.background = "url(../map/image/ico_map9.png) no-repeat";
  153. arrows.style.backgroundPosition = "0px 0px";
  154. this.style.zIndex = 0;
  155. }
  156. mp.getPanes().labelPane.appendChild(div);
  157. return div;
  158. }
  159. ComplexCustomOverlays.prototype.draw = function(){
  160. var map = this._map;
  161. var pixel = map.pointToOverlayPixel(this._point);
  162. this._div.style.left = pixel.x - parseInt(this._arrows.style.left) - 8 + "px";
  163. this._div.style.top = pixel.y - 35+ "px";
  164. };
  165. ComplexCustomOverlays.prototype.addEventListener = function(event,fun){
  166. this._div['on'+event] = fun;
  167. };
  168. /*---------------------------------------新添加的省、区域-------------------------------------------------------------*/
  169. function labelFu(data){
  170. var datacenter = data; //数据重新赋值
  171. var rank = datacenter.rank; //获取级别
  172. var text = datacenter.city; //获取城市
  173. var count = datacenter.count; //获取城市楼盘数量
  174. var point = newPoint(datacenter.point);
  175. var _points = new BMap.Point(point[0],point[1]); //经纬度处理
  176. // 复杂的自定义覆盖物
  177. /*添加图标*/
  178. function ComplexCustomOverlay(point){
  179. this._point = point;
  180. }
  181. ComplexCustomOverlay.prototype = new BMap.Overlay();
  182. ComplexCustomOverlay.prototype.initialize = function(map){
  183. this._map = map;
  184. var div = this._div = document.createElement("div");
  185. div.style.position = "absolute";
  186. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);//聚合功能?
  187. div.style.height = "45px";
  188. div.style.width="45px";
  189. div.style.cursor="pointer";
  190. div.style.zIndex = 0;
  191. div.setAttribute("pointX", this._point.lng);
  192. div.setAttribute("pointY", this._point.lat);
  193. div.setAttribute("rank", rank);
  194. // console.log(rank)
  195. var arrow = this._arrow = document.createElement("p");
  196. arrow.style.background = "url(/image/yuan.png) no-repeat 0px 0px/55px 110px";
  197. arrow.style.backgroundPosition = "0px 0x";
  198. arrow.style.width = "55px";
  199. arrow.style.height = "55px";
  200. arrow.style.lineHeight = "20px";
  201. arrow.style.textAlign = "center";
  202. arrow.style.top = "30px";
  203. arrow.style.padding = "10px 0";
  204. arrow.style.left = "30px";
  205. // arrow.innerHTML = text;
  206. var _ew = this.arow = document.createElement("em");
  207. _ew.innerHTML = text;
  208. var _arow = this.arow = document.createElement("span");
  209. _arow.style.display = " block";
  210. _arow.innerHTML = count;
  211. arrow.appendChild(_ew);
  212. div.appendChild(arrow);
  213. arrow.appendChild(_arow);
  214. div.onmouseover = function(){
  215. arrow.style.background = "url(/image/yuan.png) no-repeat center/55px 110px";
  216. arrow.style.backgroundPosition = "0px -55px";
  217. this.style.zIndex = 99990;
  218. }
  219. div.onmouseout = function(){
  220. arrow.style.background = "url(/image/yuan.png) no-repeat center/55px 110px";
  221. arrow.style.backgroundPosition = "0px 0px";
  222. this.style.zIndex = 0;
  223. }
  224. /*
  225. * 给覆盖物添加点击事件
  226. */
  227. div.addEventListener("click", function () {
  228. /*获取当前的经纬度*/
  229. var _x = $(this).attr("pointX");
  230. var _y = $(this).attr("pointY");
  231. Apoint=new BMap.Point(_x,_y);
  232. /*获取级别*/
  233. var _rank = $(this).attr("rank");
  234. var mapRank = 14;
  235. if(_rank == 1){
  236. mapRank = 10;
  237. fuAjaxRegion(); // 把区域遍历
  238. }else if(_rank == 2){
  239. mapRank = 15;
  240. fuAjaxLabel();
  241. }
  242. // console.log(Apoint)
  243. mp.centerAndZoom(Apoint, mapRank); // 点击当前覆盖物跳转到当前省份上,放大到10级
  244. mp.clearOverlays(); // 清除地图上所有的覆盖物
  245. // fuAjaxRegion(); // 把区域遍历
  246. // mp.clearOverlays(); // 清除地图上所有的覆盖物
  247. // 获取当省份
  248. var province = $(this).find('em').text();
  249. // console.log(province)
  250. });
  251. mp.getPanes().labelPane.appendChild(div); //getPanes(),返回值:MapPane,返回地图覆盖物容器列表 labelPane呢???
  252. return div;
  253. }
  254. ComplexCustomOverlay.prototype.draw = function(){
  255. var map = this._map;
  256. var pixel = map.pointToOverlayPixel(this._point);
  257. this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
  258. this._div.style.top = pixel.y - 30 + "px";
  259. }
  260. var myCompOverlay = new ComplexCustomOverlay(_points); //添加覆盖物的经纬度
  261. mp.addOverlay(myCompOverlay); //将标注添加到地图中
  262. }
  263. /*遍历所有省份*/
  264. function fuAjax(){
  265. $.ajax({
  266. url:"/map/js/ctiy.json",
  267. type:"GET",
  268. dataType:"json",
  269. success:function(data){
  270. if(data.code == 200){
  271. // console.log(data.province)
  272. $.each(data.province,function(key,val){
  273. labelFu(val);
  274. })
  275. }
  276. },
  277. error:function(request){
  278. console.log('加载失败!');
  279. }
  280. })
  281. };
  282. fuAjax();
  283. /*遍历所有区域*/
  284. function fuAjaxRegion(){
  285. $.ajax({
  286. url:"/map/js/ctiy.json",
  287. type:"GET",
  288. dataType:"json",
  289. success:function(data){
  290. if(data.code == 200){
  291. // console.log(data.region)
  292. $.each(data.region,function(key,val){
  293. labelFu(val);
  294. })
  295. }
  296. },
  297. error:function(request){
  298. console.log('加载失败!');
  299. }
  300. })
  301. };
  302. // fuAjaxRegion();
  303. /*
  304. * 新添加标注物
  305. */
  306. function fuAjaxLabel(){
  307. $.ajax({
  308. url:"/map/js/ctiy.json",
  309. type:"GET",
  310. dataType:"json",
  311. success:function(data){
  312. if(data.code == 200){
  313. $.each(data.data,function(key,val){
  314. var point = newPoint(val.point); //把经纬度进行转化
  315. var _points = new BMap.Point(point[0],point[1]); //经纬度处理
  316. var txt = "",mouseoverTxt="";
  317. var _iw = createInfoWindow(val); //把当前的数据添加到标签中
  318. var myCompOverlay = new ComplexCustomOverlays(_points,txt, mouseoverTxt); //将覆盖物存到变量,以便下面使用。
  319. mp.addOverlay(myCompOverlay); //把标签到地图上
  320. myCompOverlay.addEventListener("click", function () { //点击出现对应自定义覆盖物
  321. mp.openInfoWindow(_iw, _points) // 点击标签弹出标签
  322. });
  323. })
  324. }
  325. },
  326. error:function(request){
  327. console.log('加载失败!');
  328. }
  329. })
  330. }
  331. // 方法2:用于监听滚轮事件
  332. //firefox使用DOMMouseScroll,其他浏览器使用mousewheel
  333. $(document).bind('mousewheel DOMMouseScroll',fullscreenScroll);
  334. function fullscreenScroll(e){
  335. var delta = -e.originalEvent.wheelDelta || e.originalEvent.detail;//firefox使用detail:下3上-3,其他浏览器使用wheelDelta:下-120上120
  336. //下滚
  337. if(delta>0){
  338. e=e || window.event;
  339. var u = mp.getZoom(); // 定义地图缩放等级的变量
  340. // console.log(u)
  341. if(u <= 9){
  342. mp.clearOverlays(); //清除地图上所有的覆盖物
  343. fuAjax();
  344. }else if(u > 9 && u <= 11) {
  345. fuAjaxRegion();
  346. mp.clearOverlays(); //清除地图上所有的覆盖物
  347. }else{
  348. mp.clearOverlays(); //清除地图上所有的覆盖物
  349. fuAjaxLabel();
  350. }
  351. }
  352. //上滚
  353. if(delta<0){
  354. e=e || window.event;
  355. var u = mp.getZoom(); // 定义地图缩放等级的变量
  356. console.log(u)
  357. if(u >= 8 && u < 10){
  358. mp.clearOverlays(); //清除地图上所有的覆盖物
  359. fuAjaxRegion();
  360. }else if(u >= 10 && u <= 16){
  361. fuAjaxLabel();
  362. mp.clearOverlays(); //清除地图上所有的覆盖物
  363. }else {
  364. mp.clearOverlays(); //清除地图上所有的覆盖物
  365. fuAjax();
  366. }
  367. }
  368. }
  369. /*--------------------------------------- 新添加的省、区域 END -------------------------------------------------------------*/
  370. lplist();
  371. function lplist(){
  372. $(".y_mapcenter_list ul").html('')
  373. var html = '';
  374. $.ajax({
  375. url: "/map/js/ctiy.json",
  376. type: "GET",
  377. dataType: "json",
  378. success: function(data) {//请求成功完成后要执行的方法
  379. // console.log(data)
  380. if(data.code == 200){
  381. $.each(data.data, function (i, data) { //左边楼盘列表
  382. var jg=data.content.pic;
  383. if (jg!='') {
  384. jg=data.content.pic;
  385. }else{
  386. jg='待定';
  387. };
  388. html+= '<li class="c" data-id="'+data.point+'">';
  389. html+= '<div class="y_tu"><a href="javascript:;"><img src="'+data.content.img+'" alt=""></a></div>';
  390. html+= '<div class="y_textmain">';
  391. html+= '<div class="y_text c">';
  392. html+= '<a href="javascript:;">'+data.title+'</a>';
  393. html+= '<span><em>'+jg+'</em></span>';
  394. html+= '</div>';
  395. html+= '<div class="y_qixian">(有效期:2017年2月11日-2018年2月11日)</div>';
  396. html+= '<div class="y_hx">户型:'+data.content.add+'</div>';
  397. html+= '<div class="y_tese c">';
  398. html+= '<i>旅游地产</i><i>旅游地产</i><i>旅游地产</i>';
  399. html+= '</div>';
  400. html+= '</div> ';
  401. html+= '</li>';
  402. });
  403. $(".y_mapcenter_list ul").append(html);
  404. }
  405. // 遍历覆盖楼盘名称
  406. function labal(obj){
  407. // console.log(obj)
  408. $.each(obj.data, function (i, obj) {
  409. var txt = obj.title;
  410. var mouseoverTxt = txt + '&nbsp;&nbsp;|&nbsp;&nbsp;' + obj.content.pic;
  411. var pintx = obj.point.split('|')[0];
  412. var pinty = obj.point.split('|')[1];
  413. var myCompOverlay = new ComplexCustomOverlays(new BMap.Point(pintx, pinty), txt, mouseoverTxt); //将覆盖物存到变量,以便下面使用。
  414. mp.addOverlay(myCompOverlay);
  415. var point = new BMap.Point(pintx, pinty);
  416. var _iw = createInfoWindow(obj);
  417. var _marker = myCompOverlay; //当初存的覆盖物变量
  418. _marker.addEventListener("click", function () { //点击出现对应自定义覆盖物
  419. mp.openInfoWindow(_iw, point);
  420. });
  421. });
  422. }
  423. $(".y_mapcenter_list ul li").click(function(){ //点击列表对应自定义覆盖物
  424. var _point = $(this).attr('data-id');
  425. var _pointx = _point.split('|')[0];
  426. var _pointy = _point.split('|')[1];
  427. var _points = new BMap.Point(_pointx, _pointy);
  428. var _i = $(this).index();
  429. var _newhou = data.data[_i];
  430. var ih = createInfoWindow(_newhou);
  431. mp.centerAndZoom(_points, 15); //对应的坐标及放大级别
  432. labal(data); //遍历所以有的点
  433. mp.openInfoWindow(ih, _points) //当前覆盖添加并展示
  434. });
  435. }
  436. });
  437. }
  438. // 处理经纬坐标
  439. function newPoint(_point){
  440. var pointArr = [];
  441. var _pointx = _point.split('|')[0];
  442. var _pointy = _point.split('|')[1];
  443. pointArr.push(_pointx,_pointy);
  444. return pointArr;
  445. }
  446. function createInfoWindow(data){ //自定义覆盖物
  447. console.log(data)
  448. var iw= new BMap.InfoWindow(
  449. '<div class="map_id" style="width:260px; height:auto; position:absolute; left:0px; top:0px;">'+
  450. '<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>'+
  451. '<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>'+
  452. '<p style="margin-top:5px;"><span><em style="font-size:18px; color:#f00; font-style:normal;">'+data.content.pic+'</em></span></p>'+
  453. '</div>'
  454. );
  455. return iw
  456. }