深圳的设计企业网站,龙泉驿网站建设,软件开发报价明细,图书馆网站建设的项目报告概述
放大或缩小是地图中最基本的功能#xff0c;本文主要介绍分析 Openlayers 中Zoom缩放控件的源码实现。
源码分析
Zoom控件继承Control类#xff0c;关于Control类#xff0c;可以参考这篇文章源码分析之Openlayers中的控件篇Control基类介绍
如果直接实例化Zoom类本文主要介绍分析 Openlayers 中Zoom缩放控件的源码实现。
源码分析
Zoom控件继承Control类关于Control类可以参考这篇文章源码分析之Openlayers中的控件篇Control基类介绍
如果直接实例化Zoom类页面上显示的是 Openlayers 中原生的样式无论是控件的标签页还是样式我们都可以通过传参进行定制。本文只讨论默认情况不传参的情况。
源码如下
class Zoom extends Control {constructor(options) {options options ? options : {};super({element: document.createElement(div),target: options.target,});const className options.className ! undefined ? options.className : ol-zoom;const delta options.delta ! undefined ? options.delta : 1;const zoomInClassName options.zoomInClassName ! undefined? options.zoomInClassName: className -in;const zoomOutClassName options.zoomOutClassName ! undefined? options.zoomOutClassName: className -out;const zoomInLabel options.zoomInLabel ! undefined ? options.zoomInLabel : ;const zoomOutLabel options.zoomOutLabel ! undefined ? options.zoomOutLabel : \u2013;const zoomInTipLabel options.zoomInTipLabel ! undefined ? options.zoomInTipLabel : Zoom in;const zoomOutTipLabel options.zoomOutTipLabel ! undefined? options.zoomOutTipLabel: Zoom out;const inElement document.createElement(button);inElement.className zoomInClassName;inElement.setAttribute(type, button);inElement.title zoomInTipLabel;inElement.appendChild(typeof zoomInLabel string? document.createTextNode(zoomInLabel): zoomInLabel);inElement.addEventListener(EventType.CLICK,this.handleClick_.bind(this, delta),false);const outElement document.createElement(button);outElement.className zoomOutClassName;outElement.setAttribute(type, button);outElement.title zoomOutTipLabel;outElement.appendChild(typeof zoomOutLabel string? document.createTextNode(zoomOutLabel): zoomOutLabel);outElement.addEventListener(EventType.CLICK,this.handleClick_.bind(this, -delta),false);const cssClasses className CLASS_UNSELECTABLE CLASS_CONTROL;const element this.element;element.className cssClasses;element.appendChild(inElement);element.appendChild(outElement);this.duration_ options.duration ! undefined ? options.duration : 250;}handleClick_(delta, event) {event.preventDefault();this.zoomByDelta_(delta);}zoomByDelta_(delta) {}
}Zoom控件参数
Zoom类中的构造函数接受一个参数options若它为空或者不传则 Openlayers 将采用默认样式。options对象可以有如下属性
className:Zoom类名默认为ol-zoomdelta:缩放地图的变化量默认为1zoomInClassName放大控件的类名默认为ol-zoom-inzoomOutClassName缩小控件的类名默认为ol-zoom-outzoomInLabel:放大控件图标默认为zoomOutLabel:缩小控件图标默认为\u2013即-zoomInTipLabel:放大控件hover时的提示默认为Zoom inzoomOutTipLabel缩小控件hover时的提示默认为Zoom out
Zoom类构造函数
Zoom类的构造函数除了根据参数options定义上述变量外还调用super({element:document.createElement(div),target:options.target})将element和target传给父类Control,默认情况下options.target为undefined,因此这个传值就是设置了this.elementdocument.createElement(div)然后就是创建放大和缩小控件的DOM元素设置属性并将它们插入到this.element。期间还会注册放大按钮和缩小按钮的两个事件,事件名为handleClick不同的是delta传参不同。最后设置了this.duration_若参数options中定义了duration属性和值则赋给this.duration_否则默认为250表示是250 毫秒
Zoom类方法介绍
Zoom类中定义了两个方法handleClick_和zoomByDelta_handleClick_就是后者的一个调用核心方法就是zoomByDelta_。
zoomByDelta_方法
zoomByDelta方法如下
zoomByDelta_(delta) {const map this.getMap();const view map.getView();if (!view) {return;}const currentZoom view.getZoom();if (currentZoom ! undefined) {const newZoom view.getConstrainedZoom(currentZoom delta); //返回被限制的级别不超出范围if (this.duration_ 0) {if (view.getAnimating()) { // 若有动画正在进行则取消动画view.cancelAnimations();}view.animate({zoom: newZoom,duration: this.duration_,easing: easeOut,});} else {view.setZoom(newZoom);}}
}在上面代码中this.getMap()实际上就是调用父类Control的getMap方法中在Map类中实例化时会遍历this.controls,调用setMap方法或者在const mapnew Map(...)后用map.addControl添加控件时会触发this.controls.addEventListener(CollectionEventType.ADD,(event) {event.element.setMap(this);},);同样地会设置setMap因此Zoom缩放控件被点击时触发zoomByDelta_,调用this.getMap是可以取到map对象然后通过map.getView获取当前视图view,继而获取当前地图的级别currentZoom进行边界判断然后放大调用view.animate,缩小调用view.setZoom
总结
本文主要介绍了Openlayers中Zoom缩放控件的源码实现讲解了缩放的核心知识。 文章转载自: http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.ltpmy.cn.gov.cn.ltpmy.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.kpbgp.cn.gov.cn.kpbgp.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.iqcge.com.gov.cn.iqcge.com http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn