123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- var BMapLib = window.BMapLib = BMapLib || {};
- var INFOBOX_AT_TOP = 1, INFOBOX_AT_RIGHT = 2, INFOBOX_AT_BOTTOM = 3, INFOBOX_AT_LEFT = 4;
- (function() {
-
- var T, baidu = T = baidu || {version: '1.5.0'};
- baidu.guid = '$BAIDU$';
-
- (function() {
- window[baidu.guid] = window[baidu.guid] || {};
- baidu.lang = baidu.lang || {};
- baidu.lang.isString = function (source) {
- return '[object String]' == Object.prototype.toString.call(source);
- };
- baidu.lang.isFunction = function (source) {
- return '[object Function]' == Object.prototype.toString.call(source);
- };
- baidu.lang.Event = function (type, target) {
- this.type = type;
- this.returnValue = true;
- this.target = target || null;
- this.currentTarget = null;
- };
- baidu.object = baidu.object || {};
- baidu.extend =
- baidu.object.extend = function (target, source) {
- for (var p in source) {
- if (source.hasOwnProperty(p)) {
- target[p] = source[p];
- }
- }
- return target;
- };
- baidu.event = baidu.event || {};
- baidu.event._listeners = baidu.event._listeners || [];
- baidu.dom = baidu.dom || {};
- baidu.dom._g = function (id) {
- if (baidu.lang.isString(id)) {
- return document.getElementById(id);
- }
- return id;
- };
- baidu._g = baidu.dom._g;
- baidu.event.on = function (element, type, listener) {
- type = type.replace(/^on/i, '');
- element = baidu.dom._g(element);
- var realListener = function (ev) {
-
-
- listener.call(element, ev);
- },
- lis = baidu.event._listeners,
- filter = baidu.event._eventFilter,
- afterFilter,
- realType = type;
- type = type.toLowerCase();
-
- if(filter && filter[type]){
- afterFilter = filter[type](element, type, realListener);
- realType = afterFilter.type;
- realListener = afterFilter.listener;
- }
-
- if (element.addEventListener) {
- element.addEventListener(realType, realListener, false);
- } else if (element.attachEvent) {
- element.attachEvent('on' + realType, realListener);
- }
-
- lis[lis.length] = [element, type, listener, realListener, realType];
- return element;
- };
- baidu.on = baidu.event.on;
- baidu.event.un = function (element, type, listener) {
- element = baidu.dom._g(element);
- type = type.replace(/^on/i, '').toLowerCase();
- var lis = baidu.event._listeners,
- len = lis.length,
- isRemoveAll = !listener,
- item,
- realType, realListener;
- while (len--) {
- item = lis[len];
- if (item[1] === type
- && item[0] === element
- && (isRemoveAll || item[2] === listener)) {
- realType = item[4];
- realListener = item[3];
- if (element.removeEventListener) {
- element.removeEventListener(realType, realListener, false);
- } else if (element.detachEvent) {
- element.detachEvent('on' + realType, realListener);
- }
- lis.splice(len, 1);
- }
- }
- return element;
- };
- baidu.un = baidu.event.un;
- baidu.dom.g = function (id) {
- if ('string' == typeof id || id instanceof String) {
- return document.getElementById(id);
- } else if (id && id.nodeName && (id.nodeType == 1 || id.nodeType == 9)) {
- return id;
- }
- return null;
- };
- baidu.g = baidu.G = baidu.dom.g;
- baidu.dom._styleFixer = baidu.dom._styleFixer || {};
- baidu.dom._styleFilter = baidu.dom._styleFilter || [];
- baidu.dom._styleFilter.filter = function (key, value, method) {
- for (var i = 0, filters = baidu.dom._styleFilter, filter; filter = filters[i]; i++) {
- if (filter = filter[method]) {
- value = filter(key, value);
- }
- }
- return value;
- };
- baidu.string = baidu.string || {};
- baidu.string.toCamelCase = function (source) {
-
- if (source.indexOf('-') < 0 && source.indexOf('_') < 0) {
- return source;
- }
- return source.replace(/[-_][^-_]/g, function (match) {
- return match.charAt(1).toUpperCase();
- });
- };
- baidu.dom.setStyle = function (element, key, value) {
- var dom = baidu.dom, fixer;
-
- element = dom.g(element);
- key = baidu.string.toCamelCase(key);
- if (fixer = dom._styleFilter) {
- value = fixer.filter(key, value, 'set');
- }
- fixer = dom._styleFixer[key];
- (fixer && fixer.set) ? fixer.set(element, value) : (element.style[fixer || key] = value);
- return element;
- };
- baidu.setStyle = baidu.dom.setStyle;
- baidu.dom.setStyles = function (element, styles) {
- element = baidu.dom.g(element);
- for (var key in styles) {
- baidu.dom.setStyle(element, key, styles[key]);
- }
- return element;
- };
- baidu.setStyles = baidu.dom.setStyles;
- baidu.browser = baidu.browser || {};
- baidu.browser.ie = baidu.ie = /msie (\d+\.\d+)/i.test(navigator.userAgent) ? (document.documentMode || + RegExp['\x241']) : undefined;
- baidu.dom._NAME_ATTRS = (function () {
- var result = {
- 'cellpadding': 'cellPadding',
- 'cellspacing': 'cellSpacing',
- 'colspan': 'colSpan',
- 'rowspan': 'rowSpan',
- 'valign': 'vAlign',
- 'usemap': 'useMap',
- 'frameborder': 'frameBorder'
- };
- if (baidu.browser.ie < 8) {
- result['for'] = 'htmlFor';
- result['class'] = 'className';
- } else {
- result['htmlFor'] = 'for';
- result['className'] = 'class';
- }
- return result;
- })();
- baidu.dom.setAttr = function (element, key, value) {
- element = baidu.dom.g(element);
- if ('style' == key){
- element.style.cssText = value;
- } else {
- key = baidu.dom._NAME_ATTRS[key] || key;
- element.setAttribute(key, value);
- }
- return element;
- };
- baidu.setAttr = baidu.dom.setAttr;
- baidu.dom.setAttrs = function (element, attributes) {
- element = baidu.dom.g(element);
- for (var key in attributes) {
- baidu.dom.setAttr(element, key, attributes[key]);
- }
- return element;
- };
- baidu.setAttrs = baidu.dom.setAttrs;
- baidu.dom.create = function(tagName, opt_attributes) {
- var el = document.createElement(tagName),
- attributes = opt_attributes || {};
- return baidu.dom.setAttrs(el, attributes);
- };
- T.undope=true;
- })();
-
- var InfoBox =
-
- BMapLib.InfoBox = function(map, content, opts) {
- this._content = content || "";
- this._isOpen = false;
- this._map = map;
- this._opts = opts = opts || {};
- this._opts.offset = opts.offset || new BMap.Size(0,0);
- this._opts.boxClass = opts.boxClass || "infoBox";
- this._opts.boxStyle = opts.boxStyle || {};
- this._opts.closeIconMargin = opts.closeIconMargin || "2px";
- this._opts.closeIconUrl = opts.closeIconUrl || MAPCONFIG.MAPPATH+"images/iw_close1d3.gif";
- this._opts.enableAutoPan = opts.enableAutoPan ? true : false;
- this._opts.align = opts.align || INFOBOX_AT_TOP;
- }
- InfoBox.prototype = new BMap.Overlay();
- InfoBox.prototype.initialize = function(map) {
- var me = this;
- var div = this._div = baidu.dom.create('div', {"class": this._opts.boxClass});
- baidu.dom.setStyles(div, this._opts.boxStyle);
-
- div.style.position = "absolute";
- this._setContent(this._content);
- var floatPane = map.getPanes().floatPane;
- floatPane.style.width = "auto";
- floatPane.appendChild(div);
-
- this._getInfoBoxSize();
-
-
-
- baidu.event.on(div,"onmousedown",function(e){
- me._stopBubble(e);
- });
- baidu.event.on(div,"onmouseover",function(e){
- me._stopBubble(e);
- });
- baidu.event.on(div,"click",function(e){
- me._stopBubble(e);
- });
- baidu.event.on(div,"dblclick",function(e){
- me._stopBubble(e);
- });
- return div;
- }
- InfoBox.prototype.draw = function() {
- this._isOpen && this._adjustPosition(this._point);
- }
-
- InfoBox.prototype.open = function(anchor){
- var me = this,poi;
- if(!this._isOpen) {
- this._map.addOverlay(this);
- this._isOpen = true;
-
- setTimeout(function(){
- me._dispatchEvent(me,"open",{"point" : me._point});
- },10);
- }
- if(anchor instanceof BMap.Point){
- poi = anchor;
-
- this._removeMarkerEvt();
- }else if(anchor instanceof BMap.Marker){
-
- if(this._marker){
- this._removeMarkerEvt();
- }
- poi = anchor.getPosition();
- this._marker = anchor;
- !this._markerDragend && this._marker.addEventListener("dragend",this._markerDragend = function(e){
- me._point = e.point;
- me._adjustPosition(me._point);
- me._panBox();
- me.show();
- });
-
- !this._markerDragging && this._marker.addEventListener("dragging",this._markerDragging = function(){
- me.hide();
- me._point = me._marker.getPosition();
- me._adjustPosition(me._point);
- });
- }
-
- this.show();
- this._point = poi;
- this._panBox();
- this._adjustPosition(this._point);
- }
-
- InfoBox.prototype.close = function(){
- if(this._isOpen){
- this._map.removeOverlay(this);
- this._div.style.display="none";
- this._isOpen = false;
- this._dispatchEvent(this,"close",{"point" : this._point});
- }
- }
-
-
-
- InfoBox.prototype.enableAutoPan = function(){
- this._opts.enableAutoPan = true;
- }
-
- InfoBox.prototype.disableAutoPan = function(){
- this._opts.enableAutoPan = false;
- }
-
- InfoBox.prototype.setContent = function(content){
- this._setContent(content);
- this._getInfoBoxSize();
- this._adjustPosition(this._point);
- }
-
- InfoBox.prototype.setPosition = function(poi){
- this._point = poi;
- this._adjustPosition(poi);
- this._removeMarkerEvt();
- }
-
- InfoBox.prototype.getPosition = function(){
- return this._point;
- }
-
- InfoBox.prototype.getOffset = function(){
- return this._opts.offset;
- },
-
- InfoBox.prototype.remove = function(){
- var me = this;
- if(this.domElement && this.domElement.parentNode){
-
- baidu.event.un(this._div.firstChild, "click", me._closeHandler());
- this.domElement.parentNode.removeChild(this.domElement);
- }
- this.domElement = null;
- this._isOpen = false;
- this.dispatchEvent("onremove");
- },
- baidu.object.extend(InfoBox.prototype,{
-
- _getCloseIcon: function(){
- var img = "<img src='"+ this._opts.closeIconUrl +"' onclick='try{this.close();}catch(e){}' alt='Close Form' align='right' style='position:absolute;right:0px;cursor:pointer;margin:"+ this._opts.closeIconMargin +"'/>";
- return img;
- },
-
- _setContent: function(content){
- if(!this._div){
- return;
- }
- var closeHtml = this._getCloseIcon();
-
- if(typeof content.nodeType === "undefined"){
- this._div.innerHTML = closeHtml + content;
- }else{
- this._div.innerHTML = closeHtml;
- this._div.appendChild(content);
- }
- this._content = content;
-
- this._addEventToClose();
- },
-
- _adjustPosition: function(poi){
- var pixel = this._getPointPosition(poi);
- var icon = this._marker && this._marker.getIcon();
-
- switch(this._opts.align){
- case INFOBOX_AT_TOP:
- if(this._marker){
- this._div.style.bottom = -(pixel.y - this._opts.offset.height - icon.anchor.height + icon.infoWindowAnchor.height) - this._marker.getOffset().height + 2 + "px";
- }else{
- this._div.style.bottom = -(pixel.y - this._opts.offset.height) + "px";
- }
- break;
- case INFOBOX_AT_BOTTOM:
- if(this._marker){
- this._div.style.top = pixel.y + this._opts.offset.height - icon.anchor.height + icon.infoWindowAnchor.height + this._marker.getOffset().height + "px";
- }else{
- this._div.style.top = pixel.y + this._opts.offset.height + "px";
- }
- break;
- }
- if (this._marker) {
- this._div.style.left = pixel.x - icon.anchor.width + this._opts.offset.width + icon.infoWindowAnchor.width - this._boxWidth / 2 + "px";
- }else{
- this._div.style.left = pixel.x - this._boxWidth / 2 + this._opts.offset.width + "px";
- }
- },
-
- _getPointPosition: function(poi){
- this._pointPosition = this._map.pointToOverlayPixel(poi);
- return this._pointPosition;
- },
-
- _getInfoBoxSize: function(){
- this._boxWidth = parseInt(this._div.offsetWidth,10);
- this._boxHeight = parseInt(this._div.offsetHeight,10);
- },
-
- _addEventToClose: function(){
- var me = this;
- baidu.event.on(this._div.firstChild, "click", me._closeHandler());
- this._hasBindEventClose = true;
- },
-
- _closeHandler: function(){
- var me = this;
- return function(e){
- me.close();
- }
- },
-
- _stopBubble: function(e){
- if(e && e.stopPropagation){
- e.stopPropagation();
- }else{
- window.event.cancelBubble = true;
- }
- },
-
- _panBox: function(){
- if(!this._opts.enableAutoPan){
- return;
- }
- var mapH = parseInt(this._map.getContainer().offsetHeight,10),
- mapW = parseInt(this._map.getContainer().offsetWidth,10),
- boxH = this._boxHeight,
- boxW = this._boxWidth;
-
- if(boxH >= mapH || boxW >= mapW){
- return;
- }
-
- if(!this._map.getBounds().containsPoint(this._point)){
- this._map.setCenter(this._point);
- }
- var anchorPos = this._map.pointToPixel(this._point),
- panTop,panBottom,panY,
-
- panLeft = boxW / 2 - anchorPos.x,
-
- panRight = boxW / 2 + anchorPos.x - mapW;
- if(this._marker){
- var icon = this._marker.getIcon();
- }
-
- switch(this._opts.align){
- case INFOBOX_AT_TOP:
-
- var h = this._marker ? icon.anchor.height + this._marker.getOffset().height - icon.infoWindowAnchor.height : 0;
- panTop = boxH - anchorPos.y + this._opts.offset.height + h + 2 ;
- break;
- case INFOBOX_AT_BOTTOM:
-
- var h = this._marker ? -icon.anchor.height + icon.infoWindowAnchor.height + this._marker.getOffset().height + this._opts.offset.height : 0;
- panBottom = boxH + anchorPos.y - mapH + h + 4;
- break;
- }
- panX = panLeft > 0 ? panLeft : (panRight > 0 ? -panRight : 0);
- panY = panTop > 0 ? panTop : (panBottom > 0 ? -panBottom : 0);
- this._map.panBy(panX,panY);
- },
- _removeMarkerEvt: function(){
- this._markerDragend && this._marker.removeEventListener("dragend", this._markerDragend);
- this._markerDragging && this._marker.removeEventListener("dragging", this._markerDragging);
- this._markerDragend = this._markerDragging = null;
- },
-
- _dispatchEvent: function(instance, type, opts) {
- type.indexOf("on") != 0 && (type = "on" + type);
- var event = new baidu.lang.Event(type);
- if (!!opts) {
- for (var p in opts) {
- event[p] = opts[p];
- }
- }
- instance.dispatchEvent(event);
- }
- });
- })();
|