var that; var mapConfig = { /* 初始化入口*/ init: function () { that = this; that.lat = lat; that.lng = lng; that.map = new BMap.Map('districtMap'); var point = new BMap.Point(that.lng, that.lat); that.map.centerAndZoom(point, 14); // 编写自定义函数,创建标注 var marker = new BMap.Marker(point); function CountOverlay(city, text){ this._city = city; this._point = point; this._text = text; } CountOverlay.prototype = new BMap.Overlay(); CountOverlay.prototype.initialize = function(map){ this._map = that.map; var div = this._div = document.createElement("div"); div.style.position = "absolute"; div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat); div.style.backgroundColor = "grey"; div.style.border = "1px solid grey"; div.style.color = "white"; div.style.height = "25px"; div.style.padding = "2px"; div.style.lineHeight = "18px"; div.style.whiteSpace = "nowrap"; div.style.MozUserSelect = "none"; div.style.fontSize = "12px" div.style.cursor = "pointer" var span = this._span = document.createElement("span"); span.style.lineHeight = "inherit"; span.style.minWidth = "25px"; span.style.textAlign = "center"; div.appendChild(span); span.appendChild(document.createTextNode(this._text)); var arrow = this._arrow = document.createElement("div"); // arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat"; arrow.style.position = "absolute"; arrow.style.width = "11px"; arrow.style.height = "10px"; arrow.style.top = "24px"; arrow.style.left = "28px"; arrow.style.overflow = "hidden"; div.appendChild(arrow); /* div.addEventListener('click', function(e){ that.datas.zoom = that.datas.zoomThreshold + 2; mp.centerAndZoom(city, that.datas.zoom) }) */ that.map.getPanes().labelPane.appendChild(div); return div; } CountOverlay.prototype.draw = function(){ var map = this._map; var pixel = map.pointToOverlayPixel(this._point); this._div.style.left = pixel.x - parseInt(this._arrow.style.left) - 13 + "px"; this._div.style.top = pixel.y - 60 + "px"; } that.map.addOverlay(new CountOverlay(city, name)); that.map.addOverlay(marker); that.map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放 that.map.citycenter = new BMap.Point(that.lng, that.lat); /* 打开银行、餐饮等tab大类*/ $('.bar,#aroundLink').on('click', 'a', function () { var $this = $(this); //$this.addClass('current').siblings().removeClass('current'); var id = $this.attr('id'); that.hidden_all_mark() that.openTypeWrap(id); }); that.openTypeWrap('bus'); }, /* 计算两点间距离*/ getShortDistance: function (lon1, lat1, lon2, lat2) { // PI var DEF_PI = 3.14159265359; // 2*PI var DEF_2PI = 6.28318530712; // PI/180.0 var DEF_PI180 = 0.01745329252; // radius of earth var DEF_R = 6370693.5; var ew1, ns1, ew2, ns2, dx, dy, dew, distance; // 角度转换为弧度 ew1 = lon1 * DEF_PI180; ns1 = lat1 * DEF_PI180; ew2 = lon2 * DEF_PI180; ns2 = lat2 * DEF_PI180; // 经度差 dew = ew1 - ew2; // 若跨东经和西经180 度,进行调整 if (dew > DEF_PI) { dew = DEF_2PI - dew; } else if (dew < -DEF_PI) { dew = DEF_2PI + dew; } // 东西方向长度(在纬度圈上的投影长度) dx = DEF_R * Math.cos(ns1) * dew; // 南北方向长度(在经度圈上的投影长度) dy = DEF_R * (ns1 - ns2); // 勾股定理求斜边长 distance = Math.sqrt(dx * dx + dy * dy); return parseInt(distance); }, /* 获取附近楼盘创建标记*/ // 设置条数:type大分类 setCount: function (type) { var that = this; var list = '#' + type + '_list'; var liCount = $(list).find('li[id]').length;; var countDom = $('#' + type + 'Count'), text = '', split = '、', unit = '个'; // 楼盘 if (type === 'hs') { // 医院 text += liCount ? liCount + unit + that.have_arr_new[type] + split : ''; /*for (var item in that.house_type) { if (that.house_type.hasOwnProperty(item)) { liCount = $('#' + item + '_list').find('li[id]').length; text += liCount ? liCount + unit + that.house_type[item] + split : ''; } }*/ } else if (that.house_near_merge[type]) { // 交通 教育 for (var i = 0; i < that.house_near_merge[type].length; i++) { if (that.house_near_merge[type][i]) { var item_traffic = that.house_near_merge[type][i]; if (type === 'business') { unit = '处'; } else if (type === 'school') { unit = '所'; } else if (type === 'school') { if (item_traffic === 'subway') { unit = '条'; } else { unit = '个'; } } liCount = $('#' + item_traffic + '_list').find('li[id]').length; text += liCount ? liCount + unit + that.have_arr_new[item_traffic] + split : ''; } } } else if (type === 'hos') { // 医院 text += liCount ? liCount + unit + that.have_arr_new[type] + split : ''; } else if (type === 'other') { text = liCount + unit; } if (text) { // 除去、 if (text.lastIndexOf(split) === text.length - 1) { text = text.substr(0, text.length - 1); } countDom.text(text); //隐藏无结果提示语 $('#' + type + '_noResult').hide(); } else { //显示无结果提示 $('#' + type + '_noResult').show(); } }, /* 按距离希尔排序*/ shellSort: function (arr) { for (var step = arr.length >> 1; step > 0; step >>= 1) { for (var i = 0; i < step; ++i) { for (var j = i + step; j < arr.length; j += step) { var k = j, value = arr[j].distance, tmp = arr[j]; while (k >= step && arr[k - step].distance > value) { arr[k] = arr[k - step]; k -= step; } arr[k] = tmp; } } } return arr; }, /* 添加标注:单一图标*/ // tag为true表示楼盘需要清除;n为索引;type为大分类 addMarker_s: function (info, tag, n, isShow) { var that = this; var latLng = new BMap.Point(info.x, info.y); // var textIndent = (n + '').length > 1 ? ' style="text-indent:2px" ' : ''; var ltext = '
'; var marker = new BMapLib.RichMarker(ltext, latLng, {anchor: new BMap.Size(0, 0)}); tag && that.toDelterMarkers.push(marker); marker.provalue = info; that.openTip[info.uid] = that.opentip(marker); that.map.addOverlay(marker); if(isShow){ marker.hide(); } return marker; }, /* 鼠标划到描点事件*/ focusmarker_b: function (marker) { var info = marker.provalue; var focusInfoWinFun = function () { var container = $(marker._container), mapDiv = container.find('.mark-icon-box'); if (mapDiv.length) { marker.setTop && marker.setTop(true); mapDiv.find('.mark-icon').addClass('mark-icon-h').end().find('.mark-tip').show(); // 恢复置顶 container.css('zIndex', Math.abs(container.css('zIndex'))); var list = $('#list' + info.uid); list.find('a').css('background', '#e8f4ff'); } }; marker.addEventListener('mouseover', focusInfoWinFun); return focusInfoWinFun; }, /* 鼠标划过描点事件*/ blurmarker_b: function (marker) { var that = this; var info = marker.provalue; var blurInfoWinFun = function () { if (that.isclick != info.uid) { var container = $(marker._container), mapDiv = container.find('.mark-icon-box'); if (mapDiv.length) { mapDiv.find('.mark-icon').removeClass('mark-icon-h').end().find('.mark-tip').hide(); // 恢复置顶 container.css('zIndex', -Math.abs(container.css('zIndex'))); var list = $('#list' + info.uid); list.find('a').css('background', '#fff'); } } }; marker.addEventListener('mouseout', blurInfoWinFun); return blurInfoWinFun; }, /* maibuilding的hover*/ hoverMarker: function (marker, flg) { var that = this; if (!marker._div) { return; } var cName = marker._div.className; var tip_price = $('#tip_price'); if (flg) { if (cName.indexOf('qp') > -1) { marker._div.className = 'qp02'; } marker._div.parentNode.style.zIndex = 1; if (tip_price.length > 0) { tip_price.css('display', 'inline'); } } else { if (that.isclick != marker.provalue.newCode) { marker._div.parentNode.style.zIndex = marker._zindex; if (cName.indexOf('qp') < 0) { return; } if (tip_price) { tip_price.css('display', 'none'); } var lclass = 'qp01'; var sTc = {0: '04', 1: '01', 2: '05', 3: '03', 4: '01'}; if ('undefined' != typeof(marker.provalue.saling) && 'undefined' != typeof(sTc[marker.provalue.saling])) { lclass = 'qp' + sTc[marker.provalue.saling]; } marker._div.className = lclass; } } }, /* 关闭详情窗口*/ closeTip: function () { var that = this; $('#maptip').css('visibility', 'hidden'); var tip_price = $('#tip_price'); if (tip_price) { tip_price.css('display', 'none'); } }, /* 弹出详情窗口****/ opentip: function (marker) { var that = this; var map = that.map; var maptipDom = $('#maptip'); if (!maptipDom) { return; } var opentheTip = function () { var info = marker.provalue; if (!info) { return; } info.x = info.x || info.baidu_coord_x || info.mapx; info.y = info.y || info.baidu_coord_y || info.mapy; if (that.isclick && that.isclick == info.uid) { that.closeTip(); return; } var point0 = new BMap.Point(marker._position.lng, marker._position.lat); that.map.centerAndZoom(point0, 14); }; marker.addEventListener('click', opentheTip); return opentheTip; }, panMap: function () { var that = this; that.map.setViewport(that.markerBounds); }, /* 打开餐饮、银行大类信息*/ openTypeWrap: function (type) { var that = this; that.createPoints_new(type); }, /* 重置所有标注-*/ reset_all_mark: function (fmaker) { var that = this; if ('undefined' != typeof that.map && 'undefined' != typeof that.house_near_p) { // 设置背景为初始白色 $('li[id^=list] a').css('background', '#fff'); // 去除描点悬浮蓝色并且隐藏所有悬浮框 var map = $('#mapObj'); map.find('.mark-icon').removeClass('mark-icon-h').end().find('.mark-tip').hide(); if (fmaker && !fmaker._div) { var info = fmaker.provalue; var container = $(fmaker._container); container.css('zIndex', Math.abs(container.css('zIndex'))); var listNew = $('#list' + info.uid); listNew.find('a').css('background', '#e8f4ff'); } } }, /* 隐藏所有标注*/ hidden_all_mark: function () { var that = this; if ('undefined' != typeof that.map && 'undefined' != typeof that.house_near_p) { for (var key in that.house_near_p) { if (that.house_near_p[key].length < 1) { return; } for (var j = 0; j < that.house_near_p[key].length; j++) { var point = that.house_near_p[key][j]; point && point.hide(); } } } }, /* 只传入类型的大类标注*/ hideOtherPoints: function (type) { var that = this; if ('undefined' == typeof that.map || 'undefined' == typeof that.house_near_p || null == type) { return; } for (var key in that.house_near_p) { if (that.house_near_p[key].length > 0) { for (var j = 0; j < that.house_near_p[key].length; j++) { var point = that.house_near_p[key][j]; if(point){ if(key == type){ point.show(); }else{ point.hide() } } } } } }, /* 显示周边楼盘标注*/ createPoints_house: function () { var that = this; that.panTo(that.map.citycenter); that.hidden_all_mark(); if (that.have_arr['hs'] == 1 && 'undefined' != typeof that.map && 'undefined' != typeof that.house_near_p) { for (var key in that.house_near_p) { if ('hs' != key) { continue; } for (var j = 0; j < that.house_near_p[key].length; j++) { var point = that.house_near_p[key][j]; point && point.show(); } break; } } else { //that.getNearHouse(); } }, /** * 创建小分类下的标注:若已经创建则显示已经创建的 * @param type 分类 * @returns null */ createPoints_new: function (type) { if (null == type) { return; } type = type.replace(/\s+/g, ''); var that = this; that.panTo(that.map.citycenter); that.hideOtherPoints(type); // 若已经存在 if (that.have_arr[type] == 1) { //that.hideOtherPoints(type); /* 刷新地图标注*/ // that.hideOtherPoints(type); /* 显示该分类下所有的内容*/ /*var typeList = $('#' + type + '_list'); // 记录滚动位置 that.topScroll = typeList.parent().scrollTop(); typeList.find('li').show(); typeList.show().siblings().hide();*/ /* 隐藏更多按钮*/ $('.mscoputup').show(); $('.mscomore').hide(); } else { /*if (type == 'hs') { //that.getNearHouse(); } else { that.getnear_s(that.have_arr_new[type], that.maxMarker_count, that.view_count, that.limit, that.maxMarker_count); }*/ that.getnear_s(type,that.maxMarker_count, that.view_count, that.limit, that.maxMarker_count); } }, panTo: function(e, a) { e && a && that.map.panTo(new BMap.Point(a, e)) }, /** * 创建大分类下的标注:若已经创建则显示已经创建的 * @param key 大分类 * @returns null */ createPoints_merge: function (key) { var that = this; that.panTo(that.map.citycenter); /* 隐藏地图上所有标注*/ that.hidden_all_mark(); var mergeArr = that.house_near_merge[key]; /* 显示标注*/ var flag = false, i, j = 0, l; for (i = 0, l = mergeArr.length; i < l; i++) { if (that.have_arr[mergeArr[i]] == 1) { flag = true; break; } } /* 显示全部*/ if (flag) { if ('undefined' != typeof that.map && 'undefined' != typeof that.house_near_p) { // 显示当前大分类下所有小分类列表 var typeList = $('#' + mergeArr[0] + '_list'); typeList.show().siblings().show(); /* 显示更多按钮 隐藏收起按钮*/ $('.mscoputup').hide(); $('.mscomore').show(); for (var k in that.house_near_p) { if (that.in_array(k, mergeArr)) { for (j = 0; j < that.house_near_p[k].length; j++) { var point = that.house_near_p[k][j]; point && (j < that.view_count ? point.show() : point.hide()); } } } for (i = 0, l = mergeArr.length; i < l; i++) { var objs = $('#' + mergeArr[i] + '_list').find('li'); for (j = that.view_count; j < that.maxMarker_count; j++) { if (null == objs[j]) { break; } $(objs[j]).hide(); } } // 滚到展开位置 typeList.parent().scrollTop(that.topScroll || 0); } } else { that.init_search_merge(key); } }, init_search_merge: function (key) { var that = this; var mergeArr = that.house_near_merge[key]; for (var i = 0, l = mergeArr.length; i < l; i++) { that.getnear_s(mergeArr[i], that.maxMarker_count, that.view_count, that.limit, that.maxMarker_count, key); } }, /** * 获取周围信息 * @param type 小分类 * @param count 需要搜索的最大个数 * @param view_count 非展开状态下显示个数 * @param limit 搜索距离(m) * @param limit_count 页面上展示最大个数 * @param parentType 大分类 * @returns null */ getnear_s: function (type, count, view_count, limit, limit_count, parentType) { var that = this; /*if (cityy == '' || cityx == '' || null == type) { return; }*/ var searchname = that.have_arr_new[type]; var onLocalSearch = function (resData) { // 娱乐由于合在了一起所以单独处理 //var isFun = that.in_array(type, that.house_near_merge['fun']); /* 判断状态是否正确*/ if (localSearch.getStatus() == BMAP_STATUS_SUCCESS) { var resultArr = []; that.focusInfoWinFuns[type] = []; that.blurInfoWinFuns[type] = []; var uidarr = []; var points = [] /* 处理百度返回信息*/ for (var i = 0; i < resData.getCurrentNumPois(); i++) { var info = resData.getPoi(i); if(info){ /* 过滤地铁中的地铁口*/ info['address'] = info['address'] ? info['address'] : ''; if ('subway' == type && (!that.array_in_array(that.type_tags[type], info.tags) || info['address'].indexOf('线') < 0)) { continue; } /* 计算距离*/ info.distance = that.getShortDistance(that.map.citycenter.lng, that.map.citycenter.lat, info.point.lng, info.point.lat); uidarr.push(info.uid); resultArr.push(info); points.push(new BMap.Point(info.point.lng,info.point.lat)) } } resData._pois = that.shellSort(resultArr); that.have_arr[type] = 1; // 所有周围娱乐数据加载完毕 var allFunIsFinished = that.have_arr['ktv'] && that.have_arr['bar'] && that.have_arr['cinema'] && that.have_arr['beauty'] && that.have_arr['cafe']; // 若是娱乐将type设为fun /*if (isFun) { type = 'fun'; searchname = '娱乐'; that.focusInfoWinFuns[type] = []; that.blurInfoWinFuns[type] = []; that.have_arr[type] = 1; !that.funData && (that.funData = []); that.funData = that.funData.concat(resData._pois); // 若娱乐都检索完毕则可以画点 if (allFunIsFinished) { resData._pois = that.shellSort(that.funData); } else { return; } }*/ that.house_near_p[type] = []; /*if (!isFun || isFun && allFunIsFinished) { that.getListAndMarker(resData._pois, limit_count, view_count, type, parentType, searchname); }*/ that.getListAndMarker(resData._pois, limit_count, view_count, type, parentType, searchname); that.map.setViewport(points); } else { // 若没搜索成功娱乐也加标识位,因为娱乐是所有加载完后再描点 /*if (isFun) { that.have_arr[type] = 1; if (that.have_arr['ktv'] && that.have_arr['bar'] && that.have_arr['cinema'] && that.have_arr['beauty'] && that.have_arr['cafe']) { that.getListAndMarker(that.funData, limit_count, view_count, type, parentType, '娱乐'); } }*/ } }; var localSearch = new BMap.LocalSearch(new BMap.Point(that.lng, that.lat), {onSearchComplete: onLocalSearch}); localSearch.setPageCapacity(count); localSearch.searchNearby(searchname, that.map.citycenter, limit); }, /** * 拼接列表并描点 * @param rsData 描点数组 * @param limit_count 页面上展示最大个数 * @param view_count 非展开状态下显示个数 * @param type 小分类 * @param mergeType 大分类 * @param searchName 小分类列表头 * @param isNotVisible 是否需要隐藏 * @returns null */ getListAndMarker: function (resData, limit_count, view_count, type, mergeType, searchName, isNotVisible) { var that = this; var s = [], l = resData.length; if (l > 0) { s.push('' + that.cut_str(result.title, 12)+ '
'); if (result.distance) { s.push('' + result.distance + '米'); } s.push('