事情时被请求到的,连系百度舆图api做一个动态展现标注变化的组件,请求舆图展现某一天的标注,时刻轴请求能够动态播放每一天的标注变化…然后我就最先coding…
若有不好的处所,还请人人指出,我也是小白中的一员,接下来最先正文
准备事情:
请求百度api密钥(详细要领我也不多写了,人人应当都邑)
相识一下百度舆图API的开辟指南和类参考文档(假如嫌贫苦的话 能够直接看Demo示例)
一、起首,先加载舆图,你能够用现实的经纬度定位、浏览器定位、依据ip定位、依据城市名定位,这个你能够本身挑选
// 建立Map实例,设置舆图许可的最小/大级别
var map = this.map;
map.centerAndZoom(new BMap.Point(121.365593, 37.528502), 15); // 初始化舆图,用城市名设置舆图中心点
//map.enableScrollWheelZoom(true); //启用滚轮放大减少
centerAndZoom(center:Point, zoom:Number)
参数诠释:
Point(lng:Number, lat:Number)
以指定的经度和纬度建立一个地舆点坐标。zoom:number
舆图缩放级别
二、标注的运用(掩盖物)
连系百度舆图api示例Demo 设置点的新图标和增加多个点示例 连系一下,就能够改写成增加多个舆图标注
//编写自定义函数,建立标注
addMarker: function(point, label, status) {
// var marker = new BMap.Marker(point);
var myIcon = new BMap.Icon("images/rubbish_" + status + ".png", new BMap.Size(32, 32), {
anchor: new BMap.Size(16, 32), //中心点设置
infoWindowAnchor: new BMap.Size(16, 4) //音讯框位置5
});
var marker = new BMap.Marker(point, {
icon: myIcon
});
TimerLine.map.addOverlay(marker);
//跳动的动画
// marker.setAnimation(BMAP_ANIMATION_BOUNCE);
marker.setAnimation(BMAP_ANIMATION_DROP);
var p = marker.getPosition();
var content = "<table>";
content = content + "<tr><td> 编号:" + label.content + "</td></tr>";
content = content + "<tr><td> 坐标:" + p.lng + "," + p.lat + "</td></tr>";
content = content + "<tr><td> 状况:" + status + "</td></tr>";
content += "</table>";
var infowindow = new BMap.InfoWindow(content);
//增加绑定事宜
addEvent(marker, 'click', getAttr);
function getAttr() {
this.openInfoWindow(infowindow);
}
}
百度舆图标注增加看懂了,接下来就是动态增加标注,而且随和日期的变化而变化,比方展现垃圾箱的动态变化,8.3号清算了色彩为绿色,8.6号没清算为赤色..
头脑:
动态:就要运用ajax异步猎取数据
变化:就要运用定时器(setInterval或许 setTimeout)
setInterval 轮回挪用
setTimeout 耽误挪用 1次 详细区分能够自行百度
动手写插件
function addEvent(dom, type, fn) {
//关于支撑DOM2级事宜处置惩罚顺序addeventListener要领的浏览器
if (dom.addEventListener) {
dom.addEventListener(type, fn, false);
} else if (dom.attachEvent) {
//关于不支撑addEventListener要领但支撑attchEvent要领的浏览器
dom.attachEvent('on' + type, fn);
} else {
//关于不支撑以上两种,但支撑on+'事宜名'的浏览器
dom['on' + type] = fn;
}
}
var TimerLine = {
data: {
containerDiv: 'timerline', //容器盒子id
datesDiv:'dates',//日期盒子id
btnsDiv:'timerlineBtns',
btns: {
play: "timerbtn-play",
stop: "timerbtn-stop",
pre:"timerbtn-pre",
next:"timerbtn-next"
},
processDiv:'processbar', //进度条div
},
protect:{
lock_play:false,
lock_stop:false,
index_label:1,
index_process:0
},
rubbish_datas: [], //用来存储ajax猎取到的数据
index: 0, //变化的index
Interval_label: null,
Interval_process:null,
map: new BMap.Map("allmap", {
minZoom: 14,
maxZoom: 20
}),
Utils: {
//编写自定义函数,建立标注
addMarker: function(point, label, status) {
// var marker = new BMap.Marker(point);
var myIcon = new BMap.Icon("images/rubbish_" + status + ".png", new BMap.Size(32, 32), {
anchor: new BMap.Size(16, 32), //中心点设置
infoWindowAnchor: new BMap.Size(16, 4) //音讯框位置5
});
var marker = new BMap.Marker(point, {
icon: myIcon
});
TimerLine.map.addOverlay(marker);
//跳动的动画
// marker.setAnimation(BMAP_ANIMATION_BOUNCE);
marker.setAnimation(BMAP_ANIMATION_DROP);
var p = marker.getPosition();
var content = "<table>";
content = content + "<tr><td> 编号:" + label.content + "</td></tr>";
content = content + "<tr><td> 坐标:" + p.lng + "," + p.lat + "</td></tr>";
content = content + "<tr><td> 状况:" + status + "</td></tr>";
content += "</table>";
var infowindow = new BMap.InfoWindow(content);
//增加绑定事宜
addEvent(marker, 'click', getAttr);
function getAttr() {
this.openInfoWindow(infowindow);
}
},
/**
* 舆图标注要领
* 参数: datas:标注物数组{date:"",info:{}}
* index:序数(日期)
* */
mapSetLabel: function(datas, n,isInterval) {
TimerLine.map.clearOverlays();
var index;
console.log(TimerLine.protect.index_label);
if(isInterval){
TimerLine.protect.index_label++;
if (TimerLine.protect.index_label >= TimerLine.rubbish_datas.length - 1) {
TimerLine.protect.index_label = TimerLine.rubbish_datas.length - 1;
clearInterval(TimerLine.Interval_label);
TimerLine.protect.lock_play=false;
}
}
if (n == null) {
if(TimerLine.protect.index_label==0){
TimerLine.protect.index_label=1
}
index = TimerLine.protect.index_label;
} else {
index = parseInt(n);
TimerLine.protect.index_label = index;
}
var info = datas[index].info;
var info_count=0;
var addMarker_Interval=setInterval(function(){
var p = info[info_count].point.split(',');
var p_x = parseFloat(p[0].toString()); //纬度
var p_y = parseFloat(p[1].toString()); //经度
//建立label标签
var label = new BMap.Label(info[info_count].title, {
offset: new BMap.Size(20, -10)
});
//建立标注点
var point = new BMap.Point(p_x, p_y);
//状况(垃圾箱状况)
var status = info[info_count].status;
//增加标注的要领
TimerLine.Utils.addMarker(point, label, status);
info_count++;
if(info_count>=info.length){
clearInterval(addMarker_Interval);
}
},0);
},
//增加日期点击事宜绑定 dates li click
bindEvent: function() {
var datesDiv = document.getElementById("dates");
addEvent(datesDiv,'click',function(e){
var event = e || window.e;
var target = event.target || event.srcElement;
for(var i=0;i<TimerLine.rubbish_datas.length;i++){
if(target.innerText==TimerLine.rubbish_datas[i].date){
//
TimerLine.protect.index_process=i;
TimerLine.protect.index_label=i;
//播放解锁
if(TimerLine.protect.lock_play) TimerLine.protect.lock_play=false;
TimerLine.Utils.mapSetLabel(TimerLine.rubbish_datas, i,false);
TimerLine.Utils.Setprocess(i,false);
return ;
}
}
})
},
//进度条转动
Setprocess:function(index,isInterval){
if(isInterval){
TimerLine.protect.index_process++;
console.log(TimerLine.protect.index_process);
console.log(TimerLine.rubbish_datas.length);
if(TimerLine.protect.index_process >= TimerLine.rubbish_datas.length-1){
TimerLine.protect.index_process = TimerLine.rubbish_datas.length-1;
clearInterval(TimerLine.Interval_process);
TimerLine.protect.lock_play=false;
}
}
var datesDiv = document.getElementById("dates");
var processDiv = document.getElementById(TimerLine.data.processDiv);
if(index==null){
processDiv.style.width =parseInt(processDiv.style.width)+datesDiv.getElementsByTagName('li')[0].offsetWidth+'px';
}else{
processDiv.style.width =datesDiv.getElementsByTagName('li')[0].offsetWidth*parseInt(index+1)+'px';
}
}
},
//TimerLine初始化
init: function() {
this.createMap();
this.ajaxCreate();
//事宜绑定
this.bindEvent();
},
createMap: function() {
// 建立Map实例,设置舆图许可的最小/大级别
var map = this.map;
map.centerAndZoom(new BMap.Point(121.365593, 37.528502), 15); // 初始化舆图,用城市名设置舆图中心点
//map.enableScrollWheelZoom(true); //启用滚轮放大减少
},
ajaxCreate: function() {
var That = this;
var containerDiv = That.data.containerDiv;
$.ajax({
type: "get",
url: "js/json.json",
dataType: 'json',
success: function(data) {
containerDiv = document.getElementById(containerDiv); //容器id
That.rubbish_datas = data.result.datas; //
//console.log(That.rubbish_datas);
That.create(containerDiv, That.rubbish_datas);
//日期时刻绑定
That.Utils.bindEvent();
}
});
},
create: function(containerDiv, datas) {
var That = this;
var datasDiv ='<div class="processcontainer"><div id="processbar" style="width:120px;"></div></div>';
// var datasDiv = '<ul id="dates" class="timerlineul dates clearfix">';
datasDiv += '<ul id="dates" class="timerlineul dates clearfix">';
for (var i = 0; i < datas.length; i++) {
datasDiv += '<li>' + datas[i].date + '</li>';
}
datasDiv += '</ul>';
document.getElementById(That.data.btnsDiv).innerHTML='<div class="timerline-btns clearfix"><div id="timerbtn-pre" class="iconfont icon-shangyishou"></div><div id="timerbtn-play" class="iconfont icon-zanting"></div><div id="timerbtn-next" class="iconfont icon-xiayishou"></div></div>'
//建立第一天的标注
this.Utils.mapSetLabel(datas, 0,false);
// console.log(TimerLine.index);
That.datas = datas;
containerDiv.innerHTML = datasDiv;
},
//播放 停息 托付事宜----时刻绑定
bindEvent: function() {
if (this.data.btns == null)
return;
var That = this;
addEvent(document.getElementById(That.data.btnsDiv), 'click', function(e) {
var event = e || window.e;
var target = event.target || event.srcElement;
//播放事宜
if (target.id == That.data.btns.play) {
if(!TimerLine.protect.lock_play){
if(TimerLine.protect.index_label >= TimerLine.rubbish_datas.length-1){
TimerLine.protect.index_label=0;
var processDiv = document.getElementById(TimerLine.data.processDiv);
var datesDiv = document.getElementById("dates");
processDiv.style.width = datesDiv.getElementsByTagName('li')[0].offsetWidth+'px';
}
if(TimerLine.protect.index_process >= TimerLine.rubbish_datas.length-1){
TimerLine.protect.index_process=0;
}
//
TimerLine.Interval_label = setInterval("TimerLine.Utils.mapSetLabel(TimerLine.rubbish_datas,null,true)", 1000);
TimerLine.Interval_process = setInterval("TimerLine.Utils.Setprocess(null,true)",1000);
$("#timerbtn-play").attr("class","iconfont icon-zanting1");
//播放桎梏
TimerLine.protect.lock_play=true;
//停息解锁
TimerLine.protect.lock_stop=false;
}else if(TimerLine.protect.lock_play){
$("#timerbtn-play").attr("class","iconfont icon-zanting");
TimerLine.Interval_label&&clearInterval(TimerLine.Interval_label);
TimerLine.Interval_process&&clearInterval(TimerLine.Interval_process);
//播放解锁
TimerLine.protect.lock_play=false;
//停息加锁
TimerLine.protect.lock_stop=true;
}
}
if(target.id == That.data.btns.pre){
if(TimerLine.protect.index_label==0) return;
TimerLine.Utils.mapSetLabel(TimerLine.rubbish_datas, TimerLine.protect.index_label-1,false);
TimerLine.Utils.Setprocess(TimerLine.protect.index_process-1,false);
TimerLine.protect.index_process=TimerLine.protect.index_process-1;
}
if(target.id == That.data.btns.next){
if(TimerLine.protect.index_label==TimerLine.rubbish_datas.length-1) return;
TimerLine.Utils.mapSetLabel(TimerLine.rubbish_datas, TimerLine.protect.index_label+1,false);
TimerLine.Utils.Setprocess(TimerLine.protect.index_process+1,false);
TimerLine.protect.index_process=TimerLine.protect.index_process+1;
}
});
}
}
TimerLine.init();
以上是我本身手写的组件代码,对设想形式相识照样平常.本来是想用原型形式写,不过在setInterval时刻,要领没法挪用原型要领,让后我就改成了单例形式
TimeLine组件引见
data:数据容器绑定
protect 庇护属性 (对播放、停息、时刻轴index、标注index)
rubbish_datas 存储ajax读取的数据
Interval_label 百度舆图标注定时器
Interval_process 时刻轴定时器
Utils 东西类
init() TimeLine初始化
createMap() 建立百度舆图
ajaxCreate() 猎取数据,建立容器(create()),时刻绑定(bindEvent())
碰到的题目:
两个定时器运转时,大众index 轻易读取毛病,一个定时器修正了index 另一个定时器还没修正,如许造成了建立标注与当前时刻不符合,
注:要将修正大众变量只管写在一个要领中。大众变量最好不要在多个要领中公用,轻易在增减的时刻涌现不必要的BUG
定时器运转到最后一天的时刻要将定时器消灭。
顺序如图:
附上预览地点
更多内容能够定阅本人微信民众号,一同开启前端小白进阶的天下!
不给 Demo 地点 是否是对不起你们。哈哈?
能够关注微信民众号 复兴百度舆图时刻轴组件 ,即可收到 Demo 的地点。