// 常見問題 $(document).ready(function(){ // 添加頁面頭部與尾部 atc_header({index:3}); // 加載數據 load_datas(); }) var current_site = []; var all_site = []; var all_maker = []; var map; var load_datas = function(){ // 當前所在地名稱 var current_prov = ''; var current_city = ''; var current_area = ''; var current_status = ''; // 百度地圖API功能 map = new BMap.Map("mapBox"); // 建立Map實例 //map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地圖,設置中心點座標和地圖級別 map.centerAndZoom('北京', 12); // map.addControl(new BMap.MapTypeControl()); //添加地圖類型控件 // map.setCurrentCity("深圳"); // 設置地圖顯示的城市 此項是必須設置的 map.enableScrollWheelZoom(true); //開啓鼠標滾輪縮放 // map.addEventListener("click", function(e){ // $('.map-station-ls li').each(function(li_obj) { // var _self_ = this; // if ($(_self_).attr('val') == id) { // $(_self_).find('.img_position_class').css({'background-position-y': '-195px'}); // } // }) // }) map.addEventListener("moveend", function(e){ // 鼠標移動,動態獲取當前 城市位置 var point = new BMap.Point(map.getCenter().lng,map.getCenter().lat);// var geoc = new BMap.Geocoder(); geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; // console.log(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); current_prov = addComp.province; current_city = addComp.city; current_area = addComp.district; // 設置當前城市 $('.view-text .text').html(current_city.replace('市','')); $('.current-city').html(current_city); load_site(1,current_prov,current_city,current_area,current_status,addMarker); }); // $('.map-station-ls li').each(function(li_obj) { // var _self_ = this; // if ($(_self_).attr('val') == id) { // $(_self_).find('.img_position_class').css({'background-position-y': '-195px'}); // } // }) }); // 搜索框功能 start var ac = new BMap.Autocomplete( //創建一個自動完成的對象 {"input" : "suggestId" ,"location" : map }); var myValue; ac.addEventListener("onconfirm", function(e) { //鼠標點擊下拉列表後的事件 var _value = e.item.value; myValue = _value.province + _value.city + _value.district + _value.street + _value.business; setPlace(); setTimeout(function(){ // 根據PP 獲取當前省市區 var point = new BMap.Point(map.getCenter().lng,map.getCenter().lat);// var geoc = new BMap.Geocoder(); geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; // console.log(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); current_prov = addComp.province; current_city = addComp.city; current_area = addComp.district; // 設置當前城市 $('.view-text .text').html(current_city.replace('市','')); $('.current-city').html(current_city); load_site(1,current_prov,current_city,current_area,current_status,addMarker); }); // // 設置當前城市 // $('.view-text .text').html(current_city.replace('市','')); // $('.current-city').html(current_city); // load_site(1,current_prov,current_city,current_area,current_status,addMarker); },500); }); /** * 搜索目的地 */ function setPlace(){ map.clearOverlays(); //清除地圖上全部覆蓋物 function myFun(){ var pp = local.getResults().getPoi(0).point; //獲取第一個智能搜索的結果 map.centerAndZoom(pp, 18); map.addOverlay(new BMap.Marker(pp)); //添加標註 // alert("當前定位城市:"+cityName); } var local = new BMap.LocalSearch(map, { //智能搜索 onSearchComplete: myFun }); local.search(myValue); } $('.search-txt').bind({ 'focus':function(){ if($(this).val() == ''){ $('.show_searchInfo').hide(); } }, 'blur' : function(){ if($(this).val() == ''){ $('.show_searchInfo').show(); } }, 'keydown':function(){ var event = event ? event : (window.event ? window.event : null); if($(this).val() != '' && event.keyCode == 13){ // 按回車,則進行關鍵字檢索 myValue = $(this).val(); setPlace(); load_site(1,current_prov,current_city,current_area,current_status,addMarker); } } }); $('.search-btn').click(function(){ if($('.search-txt').val() != ''){ myValue = $('.search-txt').val(); setPlace(); load_site(1,current_prov,current_city,current_area,current_status,addMarker); } }); // 搜索框功能 stop // 添加後臺提供的標註 start // 必須先加載數據,再定位。不然出錯 function addMarker(point,status,id,index){ var myIcon = new BMap.Icon("home/image/site_all.png", new BMap.Size(25,35),{ imageOffset: new BMap.Size(-27*index, 0) // 設置圖片偏移 }); if(status == 'yes'){ myIcon = new BMap.Icon("home/image/site_all.png", new BMap.Size(25,35),{ imageOffset: new BMap.Size(-27*index, -33) // 設置圖片偏移 }); }else if(status == 'no'){ myIcon = new BMap.Icon("home/image/site_all.png", new BMap.Size(25,35),{ imageOffset: new BMap.Size(-27*index, 0) // 設置圖片偏移 }); } var marker = new BMap.Marker(point,{icon:myIcon}); map.addOverlay(marker); // 標註懸浮/離開事件 start marker.addEventListener("mouseover",function(){ var _ma_self = this; // alert(id) // 懸浮時對應左側的顯示 $('.map-station-ls li').each(function(li_obj){ var _self_ = this; if($(_self_).attr('val') == id){ $(_self_).find('.img_position_class').css({'background-position-y':'-226px'}); }else{ $(_self_).find('.img_position_class').css({'background-position-y':'-195px'}); } }); var myIcon1 = new BMap.Icon("home/image/site_all.png", new BMap.Size(32,40),{ imageOffset: new BMap.Size(3-30*index, -71) // 設置圖片偏移 }); var marker1 = new BMap.Marker(point,{icon:myIcon1}); map.addOverlay(marker1); all_site.push({ id : id, cont : marker1 }); // 標註點擊事件 start marker1.addEventListener("click",function(){ // 因使用到函數外面的數據,未進行再封裝 click_show_window(marker,id); }); // 標註點擊事件 stop marker1.addEventListener("mouseout",function(e){ // alert(id) for(var i = 0; i < all_site.length; i++){ map.removeOverlay(all_site[i].cont); } $('.map-station-ls li').each(function(li_obj) { var _self_ = this; if ($(_self_).attr('val') == id) { $(_self_).find('.img_position_class').css({'background-position-y': '-195px'}); } }) }); }); current_site.push({ id : id, cont : point }); all_maker.push({ id : id, cont : marker }) // 標註懸浮/離開事件 stop } // 添加後臺提供的標註 stop // 瀏覽器定位 OR ip定位 start if(navigator.geolocation){ var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); //基於定位的這個點的點位建立marker map.addOverlay(mk); // 將marker做爲覆蓋物添加到map地圖上 map.panTo(r.point); //將地圖中心點移動到定位的這個點位置。注意是r.point而不是r對象。 // $('.search-txt').val('您的位置:'+r.point.lng+','+r.point.lat); var geoc = new BMap.Geocoder(); geoc.getLocation(r.point, function(rs){ var addComp = rs.addressComponents; // alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); current_prov = addComp.province; current_city = addComp.city; current_area = addComp.district; // 設置當前城市 $('.view-text .text').html(current_city.replace('市','')); $('.current-city').html(current_city); load_site(1,current_prov,current_city,current_area,'',addMarker); }); } else { // 定位失敗 // alert('failed'+this.getStatus()); // 進行IP定位 function myFun(result){ var cityName = result.name; map.setCenter(cityName); current_city = cityName; var point = new BMap.Point(result.center.lng,result.center.lat);// var geoc = new BMap.Geocoder(); geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; // alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); current_prov = addComp.province; current_city = addComp.city; current_area = addComp.district; }); // 設置當前城市 $('.view-text .text').html(current_city.replace('市','')); $('.current-city').html(current_city); load_site(1,current_prov,current_city,current_area,'',addMarker); } var myCity = new BMap.LocalCity(); myCity.get(myFun); } },{enableHighAccuracy: true}) //關於狀態碼 //BMAP_STATUS_SUCCESS 檢索成功。對應數值「0」。 //BMAP_STATUS_CITY_LIST 城市列表。對應數值「1」。 //BMAP_STATUS_UNKNOWN_LOCATION 位置結果未知。對應數值「2」。 //BMAP_STATUS_UNKNOWN_ROUTE 導航結果未知。對應數值「3」。 //BMAP_STATUS_INVALID_KEY 非法密鑰。對應數值「4」。 //BMAP_STATUS_INVALID_REQUEST 非法請求。對應數值「5」。 //BMAP_STATUS_PERMISSION_DENIED 沒有權限。對應數值「6」。(自 1.1 新增) //BMAP_STATUS_SERVICE_UNAVAILABLE 服務不可用。對應數值「7」。(自 1.1 新增) //BMAP_STATUS_TIMEOUT 超時。對應數值「8」。(自 1.1 新增) }else{ // alert("您的瀏覽器不支持地理定位"); // 進行IP定位 function myFun(result){ var cityName = result.name; map.setCenter(cityName); current_city = cityName; var point = new BMap.Point(result.center.lng,result.center.lat);// var geoc = new BMap.Geocoder(); geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; // alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); current_prov = addComp.province; current_city = addComp.city; current_area = addComp.district; }); // alert("當前定位城市:"+cityName); // 設置當前城市 $('.view-text .text').html(current_city.replace('市','')); $('.current-city').html(current_city); load_site(1,current_prov,current_city,current_area,'',addMarker); } var myCity = new BMap.LocalCity(); myCity.get(myFun); } // 瀏覽器定位 stop $('.mapbtn-fullscreen').click(function(){ // 點擊全屏時的效果 if($('.atc-mapbox-right').hasClass('fulll_screen')){ $('.atc-mapbox-right').removeClass('fulll_screen'); $('.mapbtn-fullscreen').html('全屏'); $('html').css({'overflow':'auto'}); }else{ $('.atc-mapbox-right').addClass('fulll_screen'); $('.mapbtn-fullscreen').html('退出全屏'); $('html').css({'overflow':'hidden'}); } }); $('.c_right_div').mousedown(function(){ // 點擊時,電樁向右側滑動 var l_0 = null; l_0 = setInterval(function(){ var _lef = parseInt($('.scroll-cont').css('left').replace('px','')); if(_lef + $('.scroll-cont').width() > $('.scroll-area').width()){ $('.scroll-cont').css({'left':parseInt(_lef-40)+'px'}); }else{ if(null != l_0){ window.clearInterval(l_0); } } },100); $('.c_right_div').mouseup(function(){ if(null != l_0){ window.clearInterval(l_0); } }) $('.c_right_div').mouseout(function(){ if(null != l_0){ window.clearInterval(l_0); } }) }); $('.c_left_div').mousedown(function(){ // 點擊時,電樁向左側滑動 var r_0 = null; r_0 = setInterval(function(){ if(parseInt($('.scroll-cont').css('left').replace('px','')) < 0){ $('.scroll-cont').css({'left':parseInt(parseInt($('.scroll-cont').css('left').replace('px',''))+40)+'px'}); }else{ if(null != r_0){ window.clearInterval(r_0); } } },100); $('.c_left_div').mouseup(function(){ if(null != r_0){ window.clearInterval(r_0); } }) $('.c_left_div').mouseout(function(){ if(null != r_0){ window.clearInterval(r_0); } }) }); // 隱藏電樁 close $('.div_hidden_close').click(function(){ $('.map-stations').hide(); $('.scroll-cont').html(''); }); // 城市列表切換效果 changecity(map); } var changecity = function(map){ $('#cityBox').hover( function(){$(this).addClass('citybox-hover')}, function(){$(this).removeClass('citybox-hover')} ) .click(function(){ $(this).addClass('citybox-dropdown'); return false; }) .find('.btn-close').click(function(){ $(this).parents('.citybox').removeClass('citybox-dropdown').removeClass('citybox-hover'); return false; }); $('#cityBox').find('.city-a').click(function(){ // 切換城市 並定位 $(this).parents('.citybox').find('.text').text($(this).text()); $(this).parents('.citybox').find('.current-city').text($(this).text()+'市'); $(this).parents('.citybox').find('.btn-close').click(); // 定位 並獲取城市內的 區域(數據庫查詢) map.centerAndZoom($(this).text(),11); return false; }); $(document).click(function(){ $('.citybox').removeClass('citybox-hover').removeClass('citybox-dropdown'); }) } /** * 查詢站點信息及數據 * @param page // 當前顯示頁 * @param current_prov // 當前省份 * @param current_city // 當前城市 * @param current_area // 當前區域 * @param site_stauts // 當前狀態 * @param addMarker // 引入方法 */ var load_site = function(page,current_prov,current_city,current_area,site_stauts,addMarker){ var count_ = 0; count_ = count_d(); // 分頁的須要先查詢全部數據條數再進行分頁 load_site_page(page,current_prov,current_city,current_area,site_stauts,addMarker); load_a_page(count_,page,current_prov,current_city,current_area,site_stauts,addMarker); } /** * 加載數據總條數 * @returns {number} */ var count_d = function(){ var count_ = 0; // 加載總共多少條數據 $.ajax({ type: 'post', url: 'vmdata/list/list.json', // data: dataPara, dataType:'text', async: false, success: function(msg){ var obj = eval( '(' + msg + ')' );//轉換後的JSON對象 count_ = 120; } }); return count_; } var load_site_page = function(page,current_prov,current_city,current_area,site_stauts,addMarker){ var dataPara = { p : page, s : '10', current_prov : current_prov, current_city : current_city, current_area : current_area, site_stauts : site_stauts }; $.ajax({ type: 'post', url: 'vmdata/map/site_list.json', async: false, dataType:'text', data: dataPara, success: function(msg){ var obj = eval( '(' + msg + ')' );//轉換後的JSON對象 var list = obj.list; var t_html_s = '<div id="site_all_info">'; t_html_s+= '<ul class="map-station-ls clearfix">'; t_html_s+= '</ul>'; t_html_s+= '<div class="atc-list-page tc pt30 pb20 clearfix">'; t_html_s+= '<span class="atc-pages">'; t_html_s+= '<a href="#" class="btn-prev"><</a>'; t_html_s+= '<a href="#" class="btn-a cur">1</a>'; t_html_s+= '<a href="#" class="btn-a">2</a>'; t_html_s+= '<a href="#" class="btn-a">3</a>'; t_html_s+= '<a href="#" class="btn-next">></a>'; t_html_s+= '</span>'; t_html_s+= '</div>'; t_html_s+= '</div>'; $('#site_all_info').replaceWith($(t_html_s)); // 清空站點信息 var _html = ''; current_site = []; all_maker = []; for(var i = 0; i < list.length; i++){ var point = new BMap.Point(list[i].lng,list[i].lat); addMarker(point,list[i].status,list[i].id,i); // 加載地圖上的標註 // 加載左側的站點信息 _html+= '<li val="'+list[i].id+'">'; if(i == 0){ _html+= '<div class="title"><a style="background-position-x:2px;background-position-y: -195px " class="img_position_class"> </a> '+list[i].site_name+'</div>'; }else{ var x_n = 2 - i*24.1; _html+= '<div class="title"><a style="background-position-x:'+x_n+'px;background-position-y: -195px " class="img_position_class"> </a> '+list[i].site_name+'</div>'; } _html+= '<div class="cont">'; _html+= '<div class="addr">地址:'+list[i].address+'</div>'; _html+= '<div class="status">'; _html+= '充電設備:<span class="mr10">剩餘 <span class="red">'+list[i].sur_device+'</span> 個</span>總共 <span class="red">'+list[i].all_device+'</span> 個'; _html+= '</div>'; _html+= '</div> '; _html+= '</li>'; } $('.map-station-ls').html(_html); var t_marker; $('.map-station-ls li').hover( function(){ $(this).find('.img_position_class').css({'background-position-y':'-226px'}); for(var i = 0; i < current_site.length; i++){ if(current_site[i].id == $(this).attr('val')){ var point_t = current_site[i].cont; var myIcon1 = new BMap.Icon("home/image/site_all.png", new BMap.Size(32,40),{ imageOffset: new BMap.Size(3-30*i, -71) // 設置圖片偏移 }); var marker1 = new BMap.Marker(point_t,{icon:myIcon1}); map.addOverlay(marker1); t_marker = marker1; all_site.push({ id : current_site[i].id, cont : marker1 }); } } }, function(){ $(this).find('.img_position_class').css({'background-position-y':'-195px'}); for(var i = 0; i < all_site.length; i++){ map.removeOverlay(all_site[i].cont); } } ); $('.map-station-ls li').bind('click',function(){ // li點擊時 彈出窗體 var t_id = $(this).attr('val'); for(var j = 0; j < all_maker.length; j++){ if(all_maker[j].id == t_id){ click_show_window(all_maker[j].cont,t_id); } } }); }, error: function (e) { console.log(e); // alert('fail'); } }) scroll_d('site_all_info'); } /** * 進行分頁 * @param count 總數據條數 * @param curr_p 當前頁 */ var load_a_page = function(count, curr_p,current_prov,current_city,current_area,site_stauts,addMarker){ // 加載總共多少頁 第二個參數爲:當前跳轉的頁 // 控制默認樣式 if(count > 10){ var show_a_num = 3; // 默認顯示a標籤個數 var curr_page_page = 1; // 默認當前顯示底部頁數的頁數 if(curr_p > show_a_num){ curr_page_page = Math.floor(curr_p%show_a_num==0?curr_p/show_a_num:curr_p/show_a_num+1); } var sum_page = Math.floor(count%10==0?count/10:count/10+1); // 計算總頁數 var sum_page_page = Math.floor(sum_page%show_a_num==0?sum_page/show_a_num:sum_page/show_a_num+1); // a 標籤的總頁數 var t_html = '<span class="atc-pages">'; t_html+= '<a class="btn-prev"><</a>'; t_html+= '<span id="a_span">'; for(var i = ((curr_page_page-1)*show_a_num+1); i <= (show_a_num*curr_page_page); i++){ if(i <= sum_page){ if(i == curr_p){ t_html+= '<a href="#" class="btn-a cur">'+i+'</a>'; }else{ t_html+= '<a href="#" class="btn-a">'+i+'</a>'; } } } t_html+= '</span>'; t_html+= '<a class="btn-next">></a>'; t_html+= '</span>'; $('.atc-list-page').html(t_html); $('.atc-list-page').show(); $('.atc-pages .btn-prev').click(function(){ // 上一頁 if(curr_page_page > 1){ curr_page_page--; t_html = ''; for(var i = ((curr_page_page-1)*show_a_num+1); i <= (show_a_num*curr_page_page); i++){ if(i <= sum_page){ if(i == curr_p){ t_html+= '<a href="#" class="btn-a cur">'+i+'</a>'; }else{ t_html+= '<a href="#" class="btn-a">'+i+'</a>'; } } } $('#a_span').html(t_html); } }); $('.atc-pages .btn-next').click(function(){ // 下一頁 if(curr_page_page < sum_page_page){ curr_page_page++; t_html = ''; for(var i = ((curr_page_page-1)*show_a_num+1); i <= (show_a_num*curr_page_page); i++){ if(i <= sum_page){ if(i == curr_p){ t_html+= '<a href="#" class="btn-a cur">'+i+'</a>'; }else{ t_html+= '<a href="#" class="btn-a">'+i+'</a>'; } } } $('#a_span').html(t_html); } }); $('#a_span').find('a').live('click',function(){ // 從新加載頁面數據 load_site_page($(this).html(),current_prov,current_city,current_area,site_stauts,addMarker); load_a_page(count_d(),$(this).html(),current_prov,current_city,current_area,site_stauts,addMarker); }); } } var click_show_window = function(marker, id){ var p = marker.getPosition(); //獲取marker的位置 // alert('id='+id+' marker的位置是' + p.lng + ',' + p.lat); // 點擊標註 顯示當前標註上的站點基本信息 查詢全部的充電電樁 var sContent = '站點正在開發中'; $.ajax({ type: 'get', url: 'vmdata/map/map_info.json', data: {id:id}, async : false, success: function(msg){ var obj = eval( '(' + msg + ')' );//轉換後的JSON對象 var list = obj.list; var det = obj.detail; setTimeout(function(){ // 顯示當前站點的全部充電樁 // 本來是進行封裝,IE調試下,封裝的有報錯 參數無效。未解 $('.map-stations').show(); // $('.scroll-area').width($('.map-stations').width() -52); $('.scroll-cont').width(120*(list.length)); $('.scroll-cont').html(''); $('.scroll-cont').css({'left':'0px'}); var t_html = ''; for(var i = 0; i < list.length; i++){ t_html+= '<div class="scroll-item">'; t_html+= '<div class="imgbox">'; t_html+= '<img src="about:blank" width="60" height="68" />'; t_html+= '</div>'; t_html+= '<div class="num">編號:'+list[i].code+'</div>'; t_html+= ' <div class="type">類型:'+list[i].type+'</div>'; if(list[i].status != '空閒'){ t_html+= '<div class="stat-icon stat-busy">'; t_html+= '<span class="text">'; t_html+= ''+list[i].status; t_html+= '<span class="p">P</span>'; t_html+= '</span>'; t_html+= '</div>'; }else{ t_html+= '<div class="stat-icon stat-idle">'; t_html+= '<span class="text">'; t_html+= ''+list[i].status; t_html+= '<span class="p">P</span>'; t_html+= '</span>'; t_html+= '</div>'; } t_html+= '</div>'; } $('.scroll-cont').html(t_html); },0); var k_num = 0; for(var i = 0; i < list.length; i++){ if(list[i].status == '空閒'){ k_num++; } } sContent= '<div class="map-infoview map-infoview1 fst">'; sContent+= '<h4>'; sContent+= ''+det.title; sContent+= '</h4>'; sContent+= '<div class="iv-cont">'; sContent+= '<div class="clearfix">'; sContent+= '<div class="imgbox">'; sContent+= '<img src="'+det.c_img+'" width="68" height="42" />'; sContent+= '</div>'; sContent+= '<div class="aside">'; sContent+= '地址:'+det.address; sContent+= '</div>'; sContent+= '</div>'; sContent+= '<div class="func fyh">'; sContent+= '<a href="#" class="l atc-btn-b mapbtn-a">查看平面圖</a>'; if(parseInt(k_num) == 0){ sContent+= '<a href="manage/cars_regcharge.html?d_id='+id+'" class="r atc-btn-a mapbtn-a">登記預定</a>'; }else{ sContent+= '<a href="manage/cars_regcharge.html?d_id='+id+'" class="r atc-btn-a mapbtn-a">登記預定</a>'; sContent+= '<a href="manage/cars_charge.html?d_id='+id+'" class="r atc-btn-a mapbtn-a">預定充電</a>'; } sContent+= '</div>'; sContent+= '</div>'; sContent+= '<div class="status-line">'; sContent+= '充電設備:'; sContent+= '<span class="mr10">剩餘<span class="red">'+k_num+'</span>個</span>'; sContent+= '總共<span class="red">'+list.length+'</span>個'; sContent+= '</div>'; sContent+= '</div>'; }, error: function () { sContent = '站點正在開發中'; } }) var infoWindow = new BMap.InfoWindow(sContent,{enableMessage:false,width:274}); // 建立信息窗口對象 marker.openInfoWindow(infoWindow); }