-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDistanceTool.min.js
More file actions
1 lines (1 loc) · 17.7 KB
/
Copy pathDistanceTool.min.js
File metadata and controls
1 lines (1 loc) · 17.7 KB
1
var BMapLib=window.BMapLib=BMapLib||{};!function(){var t=t||{guid:"$BAIDU$"};window[t.guid]={},t.extend=function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);return t},t.lang=t.lang||{},t.lang.guid=function(){return"TANGRAM__"+(window[t.guid]._counter++).toString(36)},window[t.guid]._counter=window[t.guid]._counter||1,window[t.guid]._instances=window[t.guid]._instances||{},t.lang.Class=function(e){this.guid=e||t.lang.guid(),window[t.guid]._instances[this.guid]=this},window[t.guid]._instances=window[t.guid]._instances||{},t.lang.isString=function(t){return"[object String]"==Object.prototype.toString.call(t)},t.lang.isFunction=function(t){return"[object Function]"==Object.prototype.toString.call(t)},t.lang.Class.prototype.toString=function(){return"[object "+(this._className||"Object")+"]"},t.lang.Class.prototype.dispose=function(){for(var e in delete window[t.guid]._instances[this.guid],this)t.lang.isFunction(this[e])||delete this[e];this.disposed=!0},t.lang.Event=function(t,e){this.type=t,this.returnValue=!0,this.target=e||null,this.currentTarget=null},t.lang.Class.prototype.addEventListener=function(e,i,n){if(t.lang.isFunction(i)){!this.__listeners&&(this.__listeners={});var s,o=this.__listeners;if("string"==typeof n&&n){if(/[^\w\-]/.test(n))throw"nonstandard key:"+n;i.hashCode=n,s=n}0!=e.indexOf("on")&&(e="on"+e),"object"!=typeof o[e]&&(o[e]={}),s=s||t.lang.guid(),i.hashCode=s,o[e][s]=i}},t.lang.Class.prototype.removeEventListener=function(e,i){if(t.lang.isFunction(i))i=i.hashCode;else if(!t.lang.isString(i))return;!this.__listeners&&(this.__listeners={}),0!=e.indexOf("on")&&(e="on"+e);var n=this.__listeners;n[e]&&n[e][i]&&delete n[e][i]},t.lang.Class.prototype.dispatchEvent=function(e,i){for(var n in t.lang.isString(e)&&(e=new t.lang.Event(e)),!this.__listeners&&(this.__listeners={}),i=i||{})e[n]=i[n];var s=this.__listeners,o=e.type;if(e.target=e.target||this,e.currentTarget=this,0!=o.indexOf("on")&&(o="on"+o),t.lang.isFunction(this[o])&&this[o].apply(this,arguments),"object"==typeof s[o])for(n in s[o])s[o][n].apply(this,arguments);return e.returnValue},t.lang.inherits=function(t,e,i){var n,s,o=t.prototype,a=new Function;for(n in a.prototype=e.prototype,s=t.prototype=new a,o)s[n]=o[n];t.prototype.constructor=t,t.superClass=e.prototype,"string"==typeof i&&(s._className=i)},t.dom=t.dom||{},t._g=t.dom._g=function(e){return t.lang.isString(e)?document.getElementById(e):e},t.g=t.dom.g=function(t){return"string"==typeof t||t instanceof String?document.getElementById(t):t&&t.nodeName&&(1==t.nodeType||9==t.nodeType)?t:null},t.insertHTML=t.dom.insertHTML=function(e,i,n){var s,o;return(e=t.dom.g(e)).insertAdjacentHTML?e.insertAdjacentHTML(i,n):(s=e.ownerDocument.createRange(),"AFTERBEGIN"==(i=i.toUpperCase())||"BEFOREEND"==i?(s.selectNodeContents(e),s.collapse("AFTERBEGIN"==i)):(s[(o="BEFOREBEGIN"==i)?"setStartBefore":"setEndAfter"](e),s.collapse(o)),s.insertNode(s.createContextualFragment(n))),e},t.ac=t.dom.addClass=function(e,i){e=t.dom.g(e);for(var n=i.split(/\s+/),s=e.className,o=" "+s+" ",a=0,l=n.length;a<l;a++)o.indexOf(" "+n[a]+" ")<0&&(s+=(s?" ":"")+n[a]);return e.className=s,e},t.event=t.event||{},t.event._listeners=t.event._listeners||[],t.on=t.event.on=function(e,i,n){i=i.replace(/^on/i,""),e=t._g(e);var s,o=function(t){n.call(e,t)},a=t.event._listeners,l=t.event._eventFilter,r=i;return i=i.toLowerCase(),l&&l[i]&&(r=(s=l[i](e,i,o)).type,o=s.listener),e.addEventListener?e.addEventListener(r,o,!1):e.attachEvent&&e.attachEvent("on"+r,o),a[a.length]=[e,i,n,o,r],e},t.un=t.event.un=function(e,i,n){e=t._g(e),i=i.replace(/^on/i,"").toLowerCase();for(var s,o,a,l=t.event._listeners,r=l.length,p=!n;r--;)(s=l[r])[1]!==i||s[0]!==e||!p&&s[2]!==n||(o=s[4],a=s[3],e.removeEventListener?e.removeEventListener(o,a,!1):e.detachEvent&&e.detachEvent("on"+o,a),l.splice(r,1));return e},t.preventDefault=t.event.preventDefault=function(t){t.preventDefault?t.preventDefault():t.returnValue=!1};var e=BMapLib.DistanceTool=function(e,i){e&&(this._map=e,i=i||{},this._opts=t.extend(t.extend(this._opts||{},{tips:"测距",followText:"单击确定地点,双击结束",unit:"metric",lineColor:"#ff6319",lineStroke:2,opacity:.8,lineStyle:"solid",cursor:"http://api.map.baidu.com/images/ruler.cur",secIcon:null,closeIcon:null}),i),this._followTitle=null,this._points=[],this._paths=[],this._dots=[],this._segDistance=[],this._overlays=[],this._enableMassClear=!0,this._units={metric:{name:"metric",conv:1,incon:1e3,u1:"米",u2:"公里"},us:{name:"us",conv:3.2808,incon:5279.856,u1:"英尺",u2:"英里"}},this._isOpen=!1,this._startFollowText="单击确定起点",this._movingTimerId=null,this._styles={BMapLib_diso:"height:17px;width:5px;position:absolute;background:url(http://api.map.baidu.com/images/dis_box_01.gif) no-repeat left top",BMapLib_disi:"color:#7a7a7a;position:absolute;left:5px;padding:0 4px 1px 0;line-height:17px;background:url(http://api.map.baidu.com/images/dis_box_01.gif) no-repeat right top",BMapLib_disBoxDis:"color:#ff6319;font-weight:bold"},this._opts.lineStroke<=0&&(this._opts.lineStroke=2),this._opts.opacity>1?this._opts.opacity=1:this._opts.opacity<0&&(this._opts.opacity=0),"solid"!=this._opts.lineStyle&&"dashed"!=this._opts.lineStyle&&(this._opts.lineStyle="solid"),this._units[this._opts.unit]||(this._opts.unit="metric"),this.text="测距")};t.lang.inherits(e,t.lang.Class,"DistanceTool"),e.prototype._bind=function(){this._setCursor(this._opts.cursor);var e=this;if(t.on(this._map.getContainer(),"mousemove",function(t){if(e._isOpen&&e._followTitle)if(((t=window.event||t).target||t.srcElement)==i.getDom(e._map)){e._mapMoving||e._followTitle.show();var n=i.getDrawPoint(t,!0);e._followTitle.setPosition(n)}else e._followTitle.hide()}),this._startFollowText){this._followTitle=new BMap.Label(this._startFollowText,{offset:new BMap.Size(14,16)});this._followTitle.setStyles({color:"#333",borderColor:"#ff0103"})}},e.prototype.open=function(){if(1==this._isOpen)return!0;if(!BMapLib._toolInUse){this._isOpen=!0,BMapLib._toolInUse=!0,this._mapMoving&&delete this._mapMoving;var e=this;this._binded||(this._binded=!0,this._bind(),this._map.addEventListener("moving",function(){e._hideCurrent()})),this._followTitle&&(this._map.addOverlay(this._followTitle),this._followTitle.hide());var n=function(n){var s=e._map;if(e._isOpen){n=window.event||n;var o=i.getDrawPoint(n,!0);if(e._isPointValid(o)){if(e._bind.initX=n.pageX||n.clientX||0,e._bind.initY=n.pageY||n.clientY||0,e._points.length>0){var a=s.pointToPixel(e._points[e._points.length-1]),l=s.pointToPixel(o);if(Math.sqrt(Math.pow(a.x-l.x,2)+Math.pow(a.y-l.y,2))<5)return}e._bind.x=n.layerX||n.offsetX||0,e._bind.y=n.layerY||n.offsetY||0,e._points.push(o),e._addSecPoint(o),0==e._paths.length&&e._formatTitle(1,e._opts.followText,e._getTotalDistance()),e._paths.length>0&&(e._paths[e._paths.length-1].show(),e._paths[e._paths.length-1].setStrokeOpacity(e._opts.opacity));var r=new BMap.Polyline([o,o],{enableMassClear:e._enableMassClear});if(e._map.addOverlay(r),e._paths.push(r),e._overlays.push(r),r.setStrokeWeight(e._opts.lineStroke),r.setStrokeColor(e._opts.lineColor),r.setStrokeOpacity(e._opts.opacity/2),r.setStrokeStyle(e._opts.lineStyle),e._mapMoving&&r.hide(),e._points.length>1)e._paths[e._points.length-2].setPositionAt(1,o);var p="";if(e._points.length>1){e._setSegDistance(e._points[e._points.length-2],e._points[e._points.length-1]);var _=e._getTotalDistance();p=e._formatDisStr(_)}else p="起点";var h=new BMap.Label(p,{offset:new BMap.Size(10,-5),enableMassClear:e._enableMassClear});h.setStyles({color:"#333",borderColor:"#ff0103"}),e._map.addOverlay(h),e._formatSegLabel(h,p),e._overlays.push(h),o.disLabel=h,h.setPosition(o);var c=new t.lang.Event("onaddpoint");c.point=o,c.pixel=e._map.pointToPixel(o),c.index=e._points.length-1,c.distance=e._getTotalDistance().toFixed(0),e.dispatchEvent(c)}}},s=function(t){if(e._isOpen)if(e._paths.length>0){var n=(t=window.event||t).pageX||t.clientX||0,o=t.pageY||t.clientY||0;void 0===e._bind.initX&&(e._bind.x=t.layerX||t.offsetX||0,e._bind.y=t.layerY||t.offsetY||0,e._bind.initX=n,e._bind.initY=o);var a=e._bind.x+n-e._bind.initX,l=e._bind.y+o-e._bind.initY,r=e._paths[e._paths.length-1],p=e._map.pixelToPoint(new BMap.Pixel(a,l));r.setPositionAt(1,p),e._mapMoving||r.show();var _=0,h=0;if(a<10?_=8:a>e._map.getSize().width-10&&(_=-8),l<10?h=8:l>e._map.getSize().height-10&&(h=-8),0!=_||0!=h)s._movingTimerId||(e._mapMoving=!0,e._map.panBy(_,h,{noAnimation:!0}),e._movingTimerId=s._movingTimerId=setInterval(function(){e._map.panBy(_,h,{noAnimation:!0})},30),r.hide(),e._followTitle&&e._followTitle.hide());else if(s._movingTimerId){clearInterval(s._movingTimerId),delete s._movingTimerId,delete e._movingTimerId;var c=e._paths[e._paths.length-1],u=e._map.pixelToPoint(new BMap.Pixel(a,l));if(!c)return;c.setPositionAt(1,u),c.show(),e._followTitle&&(e._followTitle.setPosition(u),e._followTitle.show()),e._bind.i=0,e._bind.j=0,delete e._mapMoving}if(e._followTitle){var d=e._getTotalDistance(),g=e._map.getDistance(e._points[e._points.length-1],p);e._updateInstDis(e._followTitle,d+g)}}else{if(e._followTitle)e._followTitle.show(),((t=window.event||t).target||t.srcElement)!=i.getDom()&&e._followTitle.hide()}},o=function(r){e._isOpen&&(t.un(i.getDom(e._map),"click",n),t.un(document,"mousemove",s),t.un(i.getDom(e._map),"dblclick",o),t.un(document,"keydown",a),t.un(i.getDom(e._map),"mouseup",l),setTimeout(function(){e.close()},50))},a=function(t){27==(t=window.event||t).keyCode&&(e._clearCurData(),setTimeout(function(){e.close()},50))},l=function(t){t=window.event||t;var i=0;/msie (\d+\.\d)/i.test(navigator.userAgent)&&(i=document.documentMode||+RegExp.$1),(i&&1!=t.button||2==t.button)&&e.close()};return e._initData(),this._formatTitle(),i.show(this._map),this._setCursor(this._opts.cursor),t.on(i.getDom(this._map),"click",n),t.on(document,"mousemove",s),t.on(i.getDom(this._map),"dblclick",o),t.on(document,"keydown",a),t.on(i.getDom(this._map),"mouseup",l),this.bindFunc=[{elem:i.getDom(this._map),type:"click",func:n},{elem:i.getDom(this._map),type:"dblclick",func:o},{elem:document,type:"mousemove",func:s},{elem:document,type:"keydown",func:a},{elem:i.getDom(this._map),type:"mouseup",func:l}],!0}},e.prototype._dispatchLastEvent=function(){var e=new t.lang.Event("ondrawend");e.points=this._points?this._points.slice(0):[],e.overlays=this._paths?this._paths.slice(0,this._paths.length-1):[],e.distance=this._getTotalDistance().toFixed(0),this.dispatchEvent(e)},e.prototype.close=function(){if(0!=this._isOpen){this._isOpen=!1,BMapLib._toolInUse=!1,this._mapMoving&&delete this._mapMoving;var e=this;if(e._dispatchLastEvent(),e._points.length<2)e._clearCurData();else{e._paths[e._paths.length-1].remove(),e._paths[e._paths.length-1]=null,e._paths.length=e._paths.length-1;var n=e._points[e._points.length-1];n.disLabel&&n.disLabel.remove(),e._processLastOp()}i.hide();for(var s=0,o=this.bindFunc.length;s<o;s++)t.un(this.bindFunc[s].elem,this.bindFunc[s].type,this.bindFunc[s].func);e._movingTimerId&&(clearInterval(e._movingTimerId),e._movingTimerId=null),this._followTitle&&this._followTitle.hide()}},e.prototype._clearCurData=function(){for(var t=0,e=this._points.length;t<e;t++)this._points[t].disLabel&&this._points[t].disLabel.remove();for(t=0,e=this._paths.length;t<e;t++)this._paths[t].remove();for(t=0,e=this._dots.length;t<e;t++)this._dots[t].remove();this._initData()},e.prototype._initData=function(){this._points.length=0,this._paths.length=0,this._segDistance.length=0,this._dots.length=0},e.prototype._setSegDistance=function(t,e){if(t&&e){var i=this._map.getDistance(t,e);return this._segDistance.push(i),i}},e.prototype._getTotalDistance=function(){for(var t=0,e=0,i=this._segDistance.length;e<i;e++)t+=this._segDistance[e];return t},e.prototype._convertUnit=function(t,e){return e=e||"metric",this._units[e]?t*this._units[e].conv:t},e.prototype._addSecPoint=function(t){var e;e=this._opts.secIcon?this._opts.secIcon:"4.0"===BMap.apiVersion?new BMap.Icon("//mapopen.bj.bcebos.com/cms/images/mapctrls.png",new BMap.Size(12,12),{imageSize:new BMap.Size(37,442),imageOffset:new BMap.Size(25,312)}):new BMap.Icon("http://api.map.baidu.com/images/mapctrls.png",new BMap.Size(11,11),{imageOffset:new BMap.Size(-26,-313)});var i=new BMap.Marker(t,{icon:e,clickable:!1,baseZIndex:35e5,zIndexFixed:!0,enableMassClear:this._enableMassClear});this._map.addOverlay(i),this._dots.push(i)},e.prototype._formatDisStr=function(t){var e=this._opts.unit,i=this._units[e].u1,n=this._convertUnit(t,e);return n>this._units[e].incon?(n/=this._units[e].incon,i=this._units[e].u2,n=n.toFixed(1)):n=n.toFixed(0),n+i},e.prototype._setCursor=function(t){var e=/webkit/.test(navigator.userAgent.toLowerCase())?"url("+this._opts.cursor+") 3 6, crosshair":"url("+this._opts.cursor+"), crosshair";i._setCursor(e)},e.prototype._getCursor=function(){return this._opts.cursor},e.prototype._formatSegLabel=function(t,e){t.setStyle({border:"none",padding:"0"}),t.setContent("<span style='"+this._styles.BMapLib_diso+"'><span style='"+this._styles.BMapLib_disi+"'>"+e+"</span></span>")},e.prototype._processLastOp=function(){var e=this;if(delete e._bind.x,delete e._bind.y,delete e._bind.initX,delete e._bind.initY,e._paths.length>e._points.length-1){var i=e._paths.length-1;e._paths[i].remove(),e._paths[i]=null,e._paths.length=i}var s={};s.points=e._points.slice(0),s.paths=e._paths.slice(0),s.dots=e._dots.slice(0),s.segDis=e._segDistance.slice(0);var o=e._map.pointToPixel(s.points[s.points.length-1]),a=e._map.pointToPixel(s.points[s.points.length-2]),l=[0,0],r=[0,0];r=o.y-a.y>=0?[-5,11]:[-5,-35],l=o.x-a.x>=0?[14,0]:[-14,0];var p,_=s.points[s.points.length-1];_.disLabel=new BMap.Label("",{offset:new BMap.Size(-15,-40),enableMassClear:e._enableMassClear}),_.disLabel.setStyles({color:"#333",borderColor:"#ff0103"}),e._map.addOverlay(_.disLabel),_.disLabel.setOffset(new BMap.Size(r[0],r[1])),_.disLabel.setPosition(_),e._formatTitle(2,"","",_.disLabel),p=this._opts.closeIcon?this._opts.closeIcon:"4.0"===BMap.apiVersion?new BMap.Icon("//mapopen.bj.bcebos.com/cms/images/mapctrls.gif",new BMap.Size(12,12),{imageSize:new BMap.Size(82,174),imageOffset:new BMap.Size(0,14)}):new BMap.Icon("http://api.map.baidu.com/images/mapctrls.gif",new BMap.Size(12,12),{imageOffset:new BMap.Size(0,-14)}),s.closeBtn=new BMap.Marker(s.points[s.points.length-1],{icon:p,offset:new BMap.Size(l[0],l[1]),baseZIndex:36e5,enableMassClear:e._enableMassClear}),e._map.addOverlay(s.closeBtn),s.closeBtn.setTitle("清除本次测距"),s.closeBtn.addEventListener("click",function(i){for(var o=0,a=s.points.length;o<a;o++)s.points[o].disLabel.remove(),s.points[o].disLabel=null;for(o=0,a=s.paths.length;o<a;o++)s.paths[o].remove(),s.paths[o]=null;for(o=0,a=s.dots.length;o<a;o++)s.dots[o].remove(),s.dots[o]=null;s.closeBtn.remove(),s.closeBtn=null,n(i);var l=new t.lang.Event("onremovepolyline");e.dispatchEvent(l)}),e._initData()},e.prototype._formatTitle=function(t,e,i,n){var s=n||this._followTitle;if(s){s.setStyle({lineHeight:"16px",zIndex:"85",padding:"3px 5px"});var o=this._startFollowText||"",a=[];if(1==t){s.setOffset(0,25);var l=this._opts.unit,r=this._units[l].u1;(p=this._convertUnit(i,l))>this._units[l].incon?(p/=this._units[l].incon,r=this._units[l].u2,p=p.toFixed(1)):p=p.toFixed(0),a.push("<span>总长:<span style='"+this._styles.BMapLib_disBoxDis+"'>"+p+"</span>"+r+"</span><br />"),a.push("<span style='color:#7a7a7a'>"+e+"</span>")}else if(2==t){var p;l=this._opts.unit,r=this._units[l].u1;(p=this._convertUnit(this._getTotalDistance(),l))>this._units[l].incon?(p/=this._units[l].incon,r=this._units[l].u2,p=p.toFixed(1)):p=p.toFixed(0),a.push("总长:<span style='"+this._styles.BMapLib_disBoxDis+"'>"+p+"</span>"+r)}else s.setOffset(0,25),a.push(o);s.setContent(a.join(""))}},e.prototype._updateInstDis=function(t,e){var i=this._opts.unit,n=this._units[i].u1;if(e>this._units[i].incon?(e/=this._units[i].incon,n=this._units[i].u2,e=e.toFixed(1)):e=e.toFixed(0),t){var s=[];s.push("<span>总长:<span style='"+this._styles.BMapLib_disBoxDis+"'>"+e+"</span>"+n+"</span><br />"),s.push("<span style='color:#7a7a7a'>"+this._opts.followText+"</span>"),t.setContent(s.join(""))}},e.prototype._hideCurrent=function(){if(this._isOpen){if(this._paths.length>0)this._paths[this._paths.length-1].hide();this._followTitle&&this._followTitle.hide()}},e.prototype._isPointValid=function(t){if(!t)return!1;var e=this._map.getBounds(),i=e.getSouthWest(),n=e.getNorthEast();return!(t.lng<i.lng||t.lng>n.lng||t.lat<i.lat||t.lat>n.lat)};var i={_map:null,_html:"<div style='background:transparent url(http://api.map.baidu.com/images/blank.gif);position:absolute;left:0;top:0;width:100%;height:100%;z-index:1000' unselectable='on'></div>",_maskElement:null,_cursor:"default",_inUse:!1,show:function(t){this._map||(this._map=t),this._inUse=!0,this._maskElement||this._createMask(t),this._maskElement.style.display="block"},_createMask:function(e){if(this._map=e,this._map){t.insertHTML(this._map.getContainer(),"beforeEnd",this._html);var i=this._maskElement=this._map.getContainer().lastChild,s=function(e){return n(e),t.preventDefault(e)};t.on(i,"mouseup",function(t){2==t.button&&s(t)}),t.on(i,"contextmenu",s),i.style.display="none"}},getDrawPoint:function(t,e){var n=(t=window.event||t).layerX||t.offsetX||0,s=t.layerY||t.offsetY||0,o=t.target||t.srcElement;if(o!=i.getDom(this._map)&&1==e)for(;o&&o!=this._map.getContainer();)0==o.clientWidth&&0==o.clientHeight&&o.offsetParent&&"td"==o.offsetParent.nodeName.toLowerCase()||(n+=o.offsetLeft,s+=o.offsetTop),o=o.offsetParent;if(!(o!=i.getDom(this._map)&&o!=this._map.getContainer()||void 0===n||void 0===s||isNaN(n)||isNaN(s)))return this._map.pixelToPoint(new BMap.Pixel(n,s))},hide:function(){this._map&&(this._inUse=!1,this._maskElement&&(this._maskElement.style.display="none"))},getDom:function(t){return this._maskElement||this._createMask(t),this._maskElement},_setCursor:function(t){this._cursor=t||"default",this._maskElement&&(this._maskElement.style.cursor=this._cursor)}};function n(t){(t=window.event||t).stopPropagation?t.stopPropagation():t.cancelBubble=!0}}();