// 百度地图-乡镇预报 var mkjson = null; var map = null; var infoBoxs = null; var infoList = null; /* 如果雨>0:有雨 否则: 0:晴 1~2:少云 3~6:少到多云 7~9:多云 10:阴天 杜哥提供20141229 */ var weatherStatus = { 0: ["晴", 0], 1: ["少云", 1], 2: ["少云", 1], 3: ["少到多云", 1], 4: ["少到多云", 1], 5: ["少到多云", 1], 6: ["少到多云", 1], 7: ["多云", 1], 8: ["多云", 1], 9: ["多云", 1], 10: ["阴天", 2], 11: ["有雨", 7] }; $(function () { $("#map_jingxh").parent('div').show(); map = new BMap.Map("map_jingxh"); map.addControl(new BMap.MapTypeControl()); //添加地图类型控件 map.centerAndZoom(new BMap.Point(112.925278, 23.069838), 10); $("#map_jingxh").parent('div').hide(); $.getJSON("/weather/data/township/township.js?t=" + Math.random(), function (json) { if (json) { infoList = json; addInfoWindow(json); } }); }); //各站点预报 function addInfoWindow(json) { if (json) { $.each(json, function (key, value) { var station = value[0].station; var name = station[0]; var longitude = station[3]; var latitude = station[4]; if (longitude == "" && latitude == "") return; mkjson = value[0];//json addMarker(longitude, latitude, 0); }); map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用 map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件 //鼠标点击任意点获取预报信息 map.addEventListener("click", function (e) { if(e.overlay != null) return; var pt = e.point; var geoc = new BMap.Geocoder(); var geocname = ""; geoc.getLocation(pt, function (rs) { var addComp = rs.addressComponents; //alert(addComp.province + "," + addComp.city + "," + addComp.district + "," + addComp.street + "," + addComp.streetNumber); geocname = addComp.province + "," + addComp.city + "," + addComp.district + "," + addComp.street + "," + addComp.streetNumber; }) if (infoBoxs != null) infoBoxs.close(); var lng = e.point.lng; var lat = e.point.lat; $.ajax({ type: "POST", url: "weather/TownshipHandler.aspx", data: { action: 'view', lng: lng, lat: lat }, dataType: "json", success: function (json) { if (json) { mkjson = json; addMarker(lng, lat, 1, geocname); } } }); }); } }; function addMarker(longitude, latitude, isopen, geocname) { //创建小狐狸 if (isopen == 1) mkjson = mkjson[0]; var name = ""; var rain = mkjson.rain; var t2mm = mkjson.t2mm; var rh2m = mkjson.rh2m; var clct = mkjson.clct; var curr_date = new Date(); var day = curr_date.getDate(); var housr = curr_date.getHours(); var strtI = 3;//去除前一天4小时 预报时间为世界时前一天12点,+8小时才还是昨天20点,再加4才是北京时间的今天00点 var iend = strtI +24; var pt7 = new BMap.Point(longitude, latitude); //地点 var marker7 = new BMap.Marker(pt7, { icon: myIcon7 }); // 创建标注 if (isopen == 1) { marker7.hide(); name = geocname; } else { name=mkjson.station[1]; } //创建信息窗口 var sContent7 = new Array(); sContent7.push("
"); sContent7.push("
" + name + "
"); sContent7.push(""); sContent7.push(""); sContent7.push(""); for (var dayi = 1; dayi < 4; dayi++) { var mint = 0; var maxt = 0; var weatherStatus_array = new Array(); for (var i = strtI; i < iend; i += 3) { var _rain = parseFloat(rain[i]); if (_rain > 0) { var _rain_tem = parseFloat(_rain - rain[i]).toFixed(1); _rain = _rain_tem <= 0 ? _rain : _rain_tem; } else _rain = 0; var _clect = Math.ceil(clct[i]); if (_rain > 0) _clect = 11; var t2mmTempMin = Math.floor(t2mm[i]); if (mint == 0) mint = t2mmTempMin; mint = t2mmTempMin < mint ? t2mmTempMin : mint; var t2mmTempMax = Math.ceil(t2mm[i]); if (maxt == 0) maxt = t2mmTempMax; maxt = t2mmTempMax > maxt ? t2mmTempMax : maxt; weatherStatus_array.push(_clect); } var weathert = getMaxValue(weatherStatus_array); var weathert_name = weatherStatus[weathert][0]; var weathert_img = weatherStatus[weathert][1]; if (dayi == 1) { var myIcon7 = new BMap.Icon("/images/weather/tuding/" + weathert_img + ".png", new BMap.Size(22, 36)); marker7.setIcon(myIcon7); marker7.setTitle(name); map.addOverlay(marker7); // 将标注添加到地图中 } sContent7.push(""); strtI = iend; iend = strtI + 24; curr_date.setDate(curr_date.getDate() + 1); } sContent7.push("
日期天气气象现象最低气温(℃)最高气温(℃)
" + curr_date.Format('MM月dd日') + "" + weathert_name + "" + mint + "" + maxt + "
"); sContent7.push("
"); var infoBox7 = new BMapLib.InfoBox(map, sContent7.join(""), { offset: { width: 10, height: 5 }, boxStyle: { width: "410px" //height: "300px" }, boxClass: "infobox", closeIconMargin: "5px 8px 0 0", closeIconUrl: "/images/iw_close1d3.gif", enableAutoPan: true, align: INFOBOX_AT_TOP }); var openInfoWinFun = function () { //如果对象非空 则infoBoxs=上一个打开对象 if (infoBoxs != null) infoBoxs.close(); //关闭之前窗口 infoBox7.open(marker7); infoBoxs = infoBox7; //获取当前对象 } marker7.addEventListener("click", openInfoWinFun); if (isopen == 1) { openInfoWinFun(); } }