define([ 'html!templates/tools/toolCurrentArea', 'modDir/service/regionSearch', 'text!data/address_baike/db.json', 'css!styles/tools/toolCurrentArea' ], function(tplLayout, regionSearchF,addressBaikeDb) { /** * 模块状态,用于存储模块的状态 例如:收起,关闭 * @type {Object} */ var status = { initialized: false, //是否初始化 modalStatus: false //界面是否弹出 }; /** * 模块数据 用于数据存储和外部调用 * @type {Object} * 数据存放 */ var modValue = { currentAreaDataResult: {}, currentCityInfo: null //当前区域json数据 } /** * 初始化并订阅事件 * @return {[type]} [description] */ function init() { if (status.initialized === false) { setLayout(); subscribe(); bindEvent(); getCurrentCity(); hideModal(); status.initialized = true; addressBaikeDb = JSON.parse(addressBaikeDb); } else { if (status.modalStatus) { hideModal(); } else { showModal(); } } }; function setLayout(){ $("#footer .tools-location").append(tplLayout); showModal(); }; /** * 设置地图到指定的区域 * 支持url解析 * @type {Function} * @param options {Object} {center:中心点,zoom:缩放比例} */ function mapToPoint(options) { if (map23DData.display.map2D) { map2DViewer.setView({ center: options.center, zoom: options.zoom }); } else if (map23DData.display.map3D) { map3DViewer.setView({ center: options.center, zoom: options.zoom }); } setTimeout(function(){ getCurrentCity(); },500); }; /** * 显示城市跳转模态窗口 * @type {Function} */ function showModal() { $("#footer .Arealinkimg").addClass('downIcon'); // $($("#provinceModal")).css({ left: '10px' }); // $($("#provinceModal")).css({ bottom:'40px' }); $($("#provinceModal")).show(); if(map23DData.display.map2D){ $("#provinceModal .popup_html").removeClass('TD'); }else{ $("#provinceModal .popup_html").addClass('TD'); } $("#cityPlaceList").mCustomScrollbar({ scrollInertia: 1000 }); $("#cityPlaceList").mCustomScrollbar('update'); status.modalStatus = true; }; /** * 隐藏城市跳转模态窗口 * @type {Function} */ function hideModal() { $("#footer .Arealinkimg").removeClass('downIcon'); $($("#provinceModal")).hide(); status.modalStatus = false; }; /** * 界面布局重置 * @type {Function} */ function layoutResize() { $("cityPlaceList").mCustomScrollbar('update'); }; /** * 获取当前城市信息 * @type {Function} * @returns {*} */ function getCurrentCity() { var center = map23DData.view.center; if(map23DData.display.map2D){ center = map2DViewer.map.getCenter(); } var zoom = map23DData.view.zoom; getPointCity(center, zoom); }; /** * 在ONEMAP.M.user中记录当前城市信息 * @type {Function} * @param data {Object} 城市名称,中心点,bounds范围 * @private */ function setONEMAPCityInfo(data) { ONEMAP.D.cityInfo.name = data.name.text; ONEMAP.D.cityInfo.center = data.center; ONEMAP.D.cityInfo.pac = data.code; ONEMAP.D.cityInfo.zoom = data.zoom; }; /** * 获取目标城市信息数据 * @type {Function} * @param center {Object} 中心点 * @param zoom {Number} 缩放比例,不同比例返回的 区域等级不一样 * @returns {*} * @private */ function getPointCity(center, zoom) { var regionSearch = new regionSearchF; regionSearch.getRegionInfo({ latLng: [center.lat, center.lng], zoom: zoom }, function(data) { modValue.currentAreaDataResult = data.data; updateCurrentAreaPanel(data.data); //updateCurrentAreaWheater(data.data); }); }; /** * 更新天气 */ function updateCurrentAreaWheater(){ var cityname = modValue.curAreaName; // 天气搜索结果 meteo.c.http.httpFunction(meteo.c.http.station, cityname, null, function (json) { if(json.length>0){ showList(json); }else{ ONEMAP.C.publisher.publish({ type: 'error', message: '无此区域天气数据' }, 'noteBar::add'); } }) }; function showList(json){ // console.log(json) /*meteo.c.popup.showPopup("" + e.currentTarget.id.split("-")[2], json[key].cname, 0, '2017-12-25 20:00:00', '2017-12-25 08:00:00');*/ } /** * 更新 当前城市信息面板 由地图拖动来驱动 * @type {Function} * @param data {Object} * @returns {*} * @private */ function updateCurrentAreaPanel(data) { if(!data){ return false; } var curAreaName = ''; var curArea = $('#provinceModal .modal-header h3'); curArea.empty(); var nextArea = $('#CurCityInfo'); nextArea.empty(); if (data.hasOwnProperty('level') && data.level.length > 0) { var i = 0; var l = data.level.length; var ABBtn = $(''); for (i; i < l; i++) { if (i !== (l - 1)) { var item = $('' + data.level[i].name.text + ' > '); item.bind("click", { d: data.level[i] }, function(event) { mapToPoint({ center: [event.data.d.center.lat, event.data.d.center.lon], zoom: event.data.d.zoom + 3, name: event.data.d.name.text }); setONEMAPCityInfo(event.data.d); }); } else if (i === (l - 1)) { var item = $('' + data.level[i].name.text + ''); modValue.curAreaName = data.level[i].name.text; curAreaName = data.level[i].name.text; if(addressBaikeDb[(addPreZero(data.level[i].code))]){ // ABBtn = $('地名百科'); // ABBtn.bind('click',function(){ // var title = $(this).attr("title"); // var bid = $(this).attr("bid"); // require(['modDir/tools/toolAddressBaike'],function(toolAddressBaike){ // toolAddressBaike.init({ // title:title, // bid:bid // }); // }); // }) } //更新ONEMAP.D.cityInfo setONEMAPCityInfo(data.level[i]); } //设置当前 城市信息 if (data.level[i].category === 'city') { modValue.currentCityInfo = data.level[i]; } item.appendTo(curArea); } $("#curLocationArea").empty().text(curAreaName); $(".addressbaike").remove(); ABBtn.insertAfter($('.tools-location')); } else { curArea.empty().html('全球'); } if (data.hasOwnProperty('next_level') && data.next_level.length > 0) { var nextLevelAry = sortBy(data.next_level, 'code'); for (var i = 0, l = nextLevelAry.length; i < l; i++) { var item = $('' + nextLevelAry[i].name.text + ''); item.bind("click", { d: nextLevelAry[i] }, function(event) { mapToPoint({ center: [event.data.d.center.lat, event.data.d.center.lon], zoom: event.data.d.zoom + 3, name: event.data.d.name.text }); setONEMAPCityInfo(event.data.d); }); item.appendTo(nextArea); } } }; function addPreZero(num) { var t = (num + '').length, s = ''; for (var i = 0; i < 11 - t; i++) { s += '0'; } return num+s; } function sortBy(arr, prop, desc) { var props = [], ret = [], i = 0, len = arr.length; if (typeof prop == 'string') { for (; i < len; i++) { var oI = arr[i]; (props[i] = new String(oI && oI[prop] || ''))._obj = oI; } } props.sort(); for (i = 0; i < len; i++) { ret[i] = props[i]._obj; } if (desc) { ret.reverse(); } return ret; }; function popup_html_css(options){ if(options == '2d'){ $("#provinceModal .popup_html").removeClass('TD'); }else if(options == '3d'){ $("#provinceModal .popup_html").addClass('TD'); } } /** * 注册监听 * @type {Function} */ function subscribe() { ONEMAP.C.publisher.subscribe(getCurrentCity, 'mapChange23D'); ONEMAP.C.publisher.subscribe(popup_html_css, 'change23D'); }; /** * 取消监听 * @type {Function} */ function unSubscribe() {}; /** * 监听数据推送 * @type {Function} */ function publish() {}; /** * 点击事件绑定 */ function bindEvent() { $("#provinceModal .modal-header .close").bind("click", function() { hideModal(); }); $("#footer .location_wheater").bind('click',function(){ updateCurrentAreaWheater(); }) $("#provinceModal .table a").bind("click", function() { var cityname = $(this).text().replace(':', ''); require(["vendorDir/data/cityData"], function(cityData) { $.each(cityData, function(index, value) { if (value.n.indexOf(cityname) >= 0) { mapToPoint({ center: L.latLng(cityData[index].latLng[0], cityData[index].latLng[1]), zoom: 10 }); return false; } }); }); }); $("#provinceModal #CityHotCity a, #provinceModal #globalCity a").bind("click", function() { var cityname = $(this).text().replace(':', ''); require(["vendorDir/data/cityData"], function(cityData) { $.each(cityData, function(index, value) { if (value.n.indexOf(cityname) >= 0) { mapToPoint({ center: L.latLng(cityData[index].latLng[0], cityData[index].latLng[1]), zoom: 10 }); return false; } }); }); }); $("#provinceModal #cityLetterList a").click(function() { if (L.Browser.ie6) { return false; } $("#provinceModal #cityPlaceList").mCustomScrollbar("scrollTo", $(this).data("role")); }); //搜索 $('#provinceModalCitySearch .input-small').bind('focus', function(e) { $(this).val(''); }); $('#provinceModalCitySearch .input-small').bind('keyup', function(e) { $('#provinceModalCitySearchResult').empty(); var cityname = $(this).val().replace(' ', ''); if (cityname === '') { $('#provinceModalCitySearchResult').css({ display: 'none' }); return false; } require(["vendorDir/data/cityData"], function(cityData) { var count = 0; $.each(cityData, function(index, value) { if (value.n.indexOf(cityname) >= 0) { var item = $('
  • ' + cityData[index].n + '
  • ').appendTo($('#provinceModalCitySearchResult')); item.on("click", { data: value }, function(e) { mapToPoint({ center: L.latLng(e.data.data.latLng[0], e.data.data.latLng[1]), zoom: 10 }); $('#provinceModalCitySearchResult').css({ display: 'none' }); }); count++ if (count > 6) { return false; } } }); if (count == 0) { $('
  • 请输入正确的城市名
  • ').appendTo($('#provinceModalCitySearchResult')) } }); $('#provinceModalCitySearchResult').css({ display: 'block' }); }); $('#provinceModalCitySearch .input-small').bind('keydown', function(e) { if (e.keyCode === 13) { $('#provinceModalCitySearchResult').empty(); var cityname = $(this).val().replace(' ', ''); if (cityname === '') { $('#provinceModalCitySearchResult').css({ display: 'none' }); return false; } require(["vendorDir/data/cityData"], function(cityData) { var count = 0; $.each(cityData, function(index, value) { if (value.n.indexOf(cityname) >= 0) { var item = $('
  • ' + cityData[index].n + '
  • ').appendTo($('#provinceModalCitySearchResult')); item.on("click", { data: value }, function(e) { mapToPoint({ center: L.latLng(e.data.data.latLng[0], e.data.data.latLng[1]), zoom: 10 }); $('#provinceModalCitySearchResult').css({ display: 'none' }); }); count++ if (count > 6) { return false; } } }); if (count == 0) { $('
  • 请输入正确的城市名
  • ').appendTo($('#provinceModalCitySearchResult')) } }); $('#provinceModalCitySearchResult').css({ display: 'block' }); } }); }; return ONEMAP.M.CurrentArea = { init:init, showModal:showModal, hideModal:hideModal }; })