SearchInfoWindow.js 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. /**
  2. * @fileoverview 百度地图的可根据当前poi点来进行周边检索、公交、驾车查询的信息窗口,对外开放。
  3. * 主入口类是<a href="symbols/BMapLib.SearchInfoWindow.html">SearchInfoWindow</a>,
  4. * 基于Baidu Map API 1.4。
  5. *
  6. * @author Baidu Map Api Group
  7. * @version 1.4
  8. */
  9. /**
  10. * @namespace BMap的所有library类均放在BMapLib命名空间下
  11. */
  12. var BMapLib = window.BMapLib = BMapLib || {};
  13. //常量,searchInfoWindow可选择的检索类型
  14. var BMAPLIB_TAB_SEARCH = 0, BMAPLIB_TAB_TO_HERE = 1, BMAPLIB_TAB_FROM_HERE = 2;
  15. (function() {
  16. //声明baidu包
  17. var T, baidu = T = baidu || {version: '1.5.0'};
  18. baidu.guid = '$BAIDU$';
  19. //以下方法为百度Tangram框架中的方法,请到http://tangram.baidu.com 查看文档
  20. (function() {
  21. window[baidu.guid] = window[baidu.guid] || {};
  22. baidu.lang = baidu.lang || {};
  23. baidu.lang.isString = function (source) {
  24. return '[object String]' == Object.prototype.toString.call(source);
  25. };
  26. baidu.lang.Event = function (type, target) {
  27. this.type = type;
  28. this.returnValue = true;
  29. this.target = target || null;
  30. this.currentTarget = null;
  31. };
  32. baidu.object = baidu.object || {};
  33. baidu.extend =
  34. baidu.object.extend = function (target, source) {
  35. for (var p in source) {
  36. if (source.hasOwnProperty(p)) {
  37. target[p] = source[p];
  38. }
  39. }
  40. return target;
  41. };
  42. baidu.event = baidu.event || {};
  43. baidu.event._listeners = baidu.event._listeners || [];
  44. baidu.dom = baidu.dom || {};
  45. baidu.dom._g = function (id) {
  46. if (baidu.lang.isString(id)) {
  47. return document.getElementById(id);
  48. }
  49. return id;
  50. };
  51. baidu._g = baidu.dom._g;
  52. baidu.event.on = function (element, type, listener) {
  53. type = type.replace(/^on/i, '');
  54. element = baidu.dom._g(element);
  55. var realListener = function (ev) {
  56. // 1. 这里不支持EventArgument, 原因是跨frame的事件挂载
  57. // 2. element是为了修正this
  58. listener.call(element, ev);
  59. },
  60. lis = baidu.event._listeners,
  61. filter = baidu.event._eventFilter,
  62. afterFilter,
  63. realType = type;
  64. type = type.toLowerCase();
  65. // filter过滤
  66. if(filter && filter[type]){
  67. afterFilter = filter[type](element, type, realListener);
  68. realType = afterFilter.type;
  69. realListener = afterFilter.listener;
  70. }
  71. // 事件监听器挂载
  72. if (element.addEventListener) {
  73. element.addEventListener(realType, realListener, false);
  74. } else if (element.attachEvent) {
  75. element.attachEvent('on' + realType, realListener);
  76. }
  77. // 将监听器存储到数组中
  78. lis[lis.length] = [element, type, listener, realListener, realType];
  79. return element;
  80. };
  81. baidu.on = baidu.event.on;
  82. baidu.event.un = function (element, type, listener) {
  83. element = baidu.dom._g(element);
  84. type = type.replace(/^on/i, '').toLowerCase();
  85. var lis = baidu.event._listeners,
  86. len = lis.length,
  87. isRemoveAll = !listener,
  88. item,
  89. realType, realListener;
  90. while (len--) {
  91. item = lis[len];
  92. if (item[1] === type
  93. && item[0] === element
  94. && (isRemoveAll || item[2] === listener)) {
  95. realType = item[4];
  96. realListener = item[3];
  97. if (element.removeEventListener) {
  98. element.removeEventListener(realType, realListener, false);
  99. } else if (element.detachEvent) {
  100. element.detachEvent('on' + realType, realListener);
  101. }
  102. lis.splice(len, 1);
  103. }
  104. }
  105. return element;
  106. };
  107. baidu.un = baidu.event.un;
  108. baidu.dom.g = function (id) {
  109. if ('string' == typeof id || id instanceof String) {
  110. return document.getElementById(id);
  111. } else if (id && id.nodeName && (id.nodeType == 1 || id.nodeType == 9)) {
  112. return id;
  113. }
  114. return null;
  115. };
  116. baidu.g = baidu.G = baidu.dom.g;
  117. baidu.string = baidu.string || {};
  118. baidu.browser = baidu.browser || {};
  119. baidu.browser.ie = baidu.ie = /msie (\d+\.\d+)/i.test(navigator.userAgent) ? (document.documentMode || + RegExp['\x241']) : undefined;
  120. baidu.dom._NAME_ATTRS = (function () {
  121. var result = {
  122. 'cellpadding': 'cellPadding',
  123. 'cellspacing': 'cellSpacing',
  124. 'colspan': 'colSpan',
  125. 'rowspan': 'rowSpan',
  126. 'valign': 'vAlign',
  127. 'usemap': 'useMap',
  128. 'frameborder': 'frameBorder'
  129. };
  130. if (baidu.browser.ie < 8) {
  131. result['for'] = 'htmlFor';
  132. result['class'] = 'className';
  133. } else {
  134. result['htmlFor'] = 'for';
  135. result['className'] = 'class';
  136. }
  137. return result;
  138. })();
  139. baidu.dom.setAttr = function (element, key, value) {
  140. element = baidu.dom.g(element);
  141. if ('style' == key){
  142. element.style.cssText = value;
  143. } else {
  144. key = baidu.dom._NAME_ATTRS[key] || key;
  145. element.setAttribute(key, value);
  146. }
  147. return element;
  148. };
  149. baidu.setAttr = baidu.dom.setAttr;
  150. baidu.dom.setAttrs = function (element, attributes) {
  151. element = baidu.dom.g(element);
  152. for (var key in attributes) {
  153. baidu.dom.setAttr(element, key, attributes[key]);
  154. }
  155. return element;
  156. };
  157. baidu.setAttrs = baidu.dom.setAttrs;
  158. baidu.dom.create = function(tagName, opt_attributes) {
  159. var el = document.createElement(tagName),
  160. attributes = opt_attributes || {};
  161. return baidu.dom.setAttrs(el, attributes);
  162. };
  163. baidu.cookie = baidu.cookie || {};
  164. baidu.cookie._isValidKey = function (key) {
  165. // http://www.w3.org/Protocols/rfc2109/rfc2109
  166. // Syntax: General
  167. // The two state management headers, Set-Cookie and Cookie, have common
  168. // syntactic properties involving attribute-value pairs. The following
  169. // grammar uses the notation, and tokens DIGIT (decimal digits) and
  170. // token (informally, a sequence of non-special, non-white space
  171. // characters) from the HTTP/1.1 specification [RFC 2068] to describe
  172. // their syntax.
  173. // av-pairs = av-pair *(";" av-pair)
  174. // av-pair = attr ["=" value] ; optional value
  175. // attr = token
  176. // value = word
  177. // word = token | quoted-string
  178. // http://www.ietf.org/rfc/rfc2068.txt
  179. // token = 1*<any CHAR except CTLs or tspecials>
  180. // CHAR = <any US-ASCII character (octets 0 - 127)>
  181. // CTL = <any US-ASCII control character
  182. // (octets 0 - 31) and DEL (127)>
  183. // tspecials = "(" | ")" | "<" | ">" | "@"
  184. // | "," | ";" | ":" | "\" | <">
  185. // | "/" | "[" | "]" | "?" | "="
  186. // | "{" | "}" | SP | HT
  187. // SP = <US-ASCII SP, space (32)>
  188. // HT = <US-ASCII HT, horizontal-tab (9)>
  189. return (new RegExp("^[^\\x00-\\x20\\x7f\\(\\)<>@,;:\\\\\\\"\\[\\]\\?=\\{\\}\\/\\u0080-\\uffff]+\x24")).test(key);
  190. };
  191. baidu.cookie.getRaw = function (key) {
  192. if (baidu.cookie._isValidKey(key)) {
  193. var reg = new RegExp("(^| )" + key + "=([^;]*)(;|\x24)"),
  194. result = reg.exec(document.cookie);
  195. if (result) {
  196. return result[2] || null;
  197. }
  198. }
  199. return null;
  200. };
  201. baidu.cookie.get = function (key) {
  202. var value = baidu.cookie.getRaw(key);
  203. if ('string' == typeof value) {
  204. value = decodeURIComponent(value);
  205. return value;
  206. }
  207. return null;
  208. };
  209. baidu.cookie.setRaw = function (key, value, options) {
  210. if (!baidu.cookie._isValidKey(key)) {
  211. return;
  212. }
  213. options = options || {};
  214. //options.path = options.path || "/"; // meizz 20100402 设定一个初始值,方便后续的操作
  215. //berg 20100409 去掉,因为用户希望默认的path是当前路径,这样和浏览器对cookie的定义也是一致的
  216. // 计算cookie过期时间
  217. var expires = options.expires;
  218. if ('number' == typeof options.expires) {
  219. expires = new Date();
  220. expires.setTime(expires.getTime() + options.expires);
  221. }
  222. document.cookie =
  223. key + "=" + value
  224. + (options.path ? "; path=" + options.path : "")
  225. + (expires ? "; expires=" + expires.toGMTString() : "")
  226. + (options.domain ? "; domain=" + options.domain : "")
  227. + (options.secure ? "; secure" : '');
  228. };
  229. baidu.cookie.set = function (key, value, options) {
  230. baidu.cookie.setRaw(key, encodeURIComponent(value), options);
  231. };
  232. baidu.cookie.remove = function (key, options) {
  233. options = options || {};
  234. options.expires = new Date(0);
  235. baidu.cookie.setRaw(key, '', options);
  236. };// 声明快捷
  237. //检验是否是正确的手机号
  238. baidu.isPhone = function(phone) {
  239. return /\d{11}/.test(phone);
  240. }
  241. //检验是否是正确的激活码
  242. baidu.isActivateCode = function(vcode) {
  243. return /\d{4}/.test(vcode);
  244. }
  245. T.undope=true;
  246. })();
  247. /**
  248. * @exports SearchInfoWindow as BMapLib.SearchInfoWindow
  249. */
  250. var SearchInfoWindow =
  251. /**
  252. * SearchInfoWindow类的构造函数
  253. * @class SearchInfoWindow <b>入口</b>。
  254. * 可以定义检索模版。
  255. * @constructor
  256. * @param {Map} map Baidu map的实例对象.
  257. * @param {String} content searchInfoWindow中的内容,支持HTML内容.
  258. * @param {Json Object} opts 可选的输入参数,非必填项。可输入选项包括:<br />
  259. * {<br />"<b>title</b>" : {String} searchInfoWindow的标题内容,支持HTML内容<br/>
  260. * {<br />"<b>width</b>" : {Number} searchInfoWindow的内容宽度<br/>
  261. * {<br />"<b>height</b>" : {Number} searchInfoWindow的内容高度 <br/>
  262. * {<br />"<b>offset</b>" : {Size} searchInfoWindow的偏移量<br/>
  263. * <br />"<b>enableAutoPan</b>" : {Boolean} 是否启动自动平移功能,默认开启 <br />
  264. * <br />"<b>panel</b>" : {String} 用来展现检索信息的面板,可以是dom元素或元素的ID值 <br />
  265. * <br />"<b>searchTypes</b>" : {Array} 通过传入数组设置检索面板的Tab选项共有三个选项卡可选择,选项卡顺序也按照数组的顺序来排序,传入空数组则不显示检索模版,不设置此参数则默认所有选项卡都显示。数组可传入的值为常量:BMAPLIB_TAB_SEARCH<周边检索>, BMAPLIB_TAB_TO_HERE<到去这里>, BMAPLIB_TAB_FROM_HERE<从这里出发> <br />
  266. * <br />"<b>enableSendToPhone</b>" : {Boolean} 是否启动发送到手机功能,默认开启 <br />
  267. * }<br />.
  268. * @example <b>参考示例:</b><br />
  269. * var searchInfoWindow = new BMapLib.SearchInfoWindow(map,"百度地图api",{
  270. * title "百度大厦",
  271. * width : 280,
  272. * height : 50,
  273. * panel : "panel", //检索结果面板
  274. * enableAutoPan : true, //自动平移
  275. * enableSendToPhone : true, //是否启动发送到手机功能
  276. * searchTypes :[
  277. * BMAPLIB_TAB_SEARCH, //周边检索
  278. * BMAPLIB_TAB_TO_HERE, //到这里去
  279. * BMAPLIB_TAB_FROM_HERE //从这里出发
  280. * ]
  281. * });
  282. */
  283. BMapLib.SearchInfoWindow = function(map, content, opts) {
  284. //存储当前实例
  285. this.guid = guid++;
  286. BMapLib.SearchInfoWindow.instance[this.guid] = this;
  287. this._isOpen = false;
  288. this._map = map;
  289. this._opts = opts = opts || {};
  290. this._content = content || "";
  291. this._opts.width = opts.width;
  292. this._opts.height = opts.height;
  293. this._opts._title = opts.title || "";
  294. this._opts.offset = opts.offset || new BMap.Size(0,0);
  295. this._opts.enableAutoPan = opts.enableAutoPan === false? false : true;
  296. this._opts._panel = opts.panel || null;
  297. this._opts._searchTypes = opts.searchTypes; //传入数组形式
  298. this._opts.enableSendToPhone = opts.enableSendToPhone; //是否开启发送到手机
  299. }
  300. SearchInfoWindow.prototype = new BMap.Overlay();
  301. SearchInfoWindow.prototype.initialize = function(map) {
  302. this._closeOtherSearchInfo();
  303. var me = this;
  304. var div = this._createSearchTemplate();
  305. var floatPane = map.getPanes().floatPane;
  306. floatPane.style.width = "auto";
  307. floatPane.appendChild(div);
  308. this._initSearchTemplate();
  309. //设置完内容后,获取div的宽度,高度
  310. this._getSearchInfoWindowSize();
  311. this._boxWidth = parseInt(this.container.offsetWidth,10);
  312. this._boxHeight = parseInt(this.container.offsetHeight,10);
  313. //阻止各种冒泡事件
  314. baidu.event.on(div,"onmousedown",function(e){
  315. me._stopBubble(e);
  316. });
  317. baidu.event.on(div,"ontouchstart",function(e){
  318. me._stopBubble(e);
  319. });
  320. baidu.event.on(div,"touchmove",function(e){
  321. me._stopBubble(e);
  322. });
  323. baidu.event.on(div,"touchend",function(e){
  324. me._stopBubble(e);
  325. });
  326. baidu.event.on(div,"onmouseover",function(e){
  327. me._stopBubble(e);
  328. });
  329. baidu.event.on(div,"click",function(e){
  330. me._stopBubble(e);
  331. });
  332. baidu.event.on(div,"dblclick",function(e){
  333. me._stopBubble(e);
  334. });
  335. return div;
  336. }
  337. SearchInfoWindow.prototype.draw = function() {
  338. this._isOpen && this._adjustPosition(this._point);
  339. }
  340. /**
  341. * 打开searchInfoWindow
  342. * @param {Marker|Point} location 要在哪个marker或者point上打开searchInfoWindow
  343. * @return none
  344. *
  345. * @example <b>参考示例:</b><br />
  346. * searchInfoWindow.open();
  347. */
  348. SearchInfoWindow.prototype.open = function(anchor){
  349. this._map.closeInfoWindow();
  350. var me = this,poi;
  351. if(!this._isOpen) {
  352. this._map.addOverlay(this);
  353. this._isOpen = true;
  354. //延迟10ms派发open事件,使后绑定的事件可以触发。
  355. setTimeout(function(){
  356. me._dispatchEvent(me,"open",{"point" : me._point});
  357. },10);
  358. }
  359. if(anchor instanceof BMap.Point){
  360. poi = anchor;
  361. //清除之前存在的marker事件绑定,如果存在的话
  362. this._removeMarkerEvt();
  363. this._marker = null;
  364. }else if(anchor instanceof BMap.Marker){
  365. //如果当前marker不为空,说明是第二个marker,或者第二次点open按钮,先移除掉之前绑定的事件
  366. if(this._marker){
  367. this._removeMarkerEvt();
  368. }
  369. poi = anchor.getPosition();
  370. this._marker = anchor;
  371. !this._markerDragend && this._marker.addEventListener("dragend",this._markerDragend = function(e){
  372. me._point = e.point;
  373. me._adjustPosition(me._point);
  374. me._panBox();
  375. me.show();
  376. });
  377. //给marker绑定dragging事件,拖动marker的时候,searchInfoWindow也跟随移动
  378. !this._markerDragging && this._marker.addEventListener("dragging",this._markerDragging = function(){
  379. me.hide();
  380. me._point = me._marker.getPosition();
  381. me._adjustPosition(me._point);
  382. });
  383. }
  384. //打开的时候,将infowindow显示
  385. this.show();
  386. this._point = poi;
  387. this._panBox();
  388. this._adjustPosition(this._point);
  389. }
  390. /**
  391. * 关闭searchInfoWindow
  392. * @return none
  393. *
  394. * @example <b>参考示例:</b><br />
  395. * searchInfoWindow.close();
  396. */
  397. SearchInfoWindow.prototype.close = function(){
  398. if(this._isOpen){
  399. this._map.removeOverlay(this);
  400. this._disposeAutoComplete();
  401. this._isOpen = false;
  402. this._dispatchEvent(this,"close",{"point" : this._point});
  403. }
  404. }
  405. /**
  406. * 打开searchInfoWindow时,派发事件的接口
  407. * @name SearchInfoWindow#Open
  408. * @event
  409. * @param {Event Object} e 回调函数会返回event参数,包括以下返回值:
  410. * <br />{"<b>target</b> : {BMap.Overlay} 触发事件的元素,
  411. * <br />"<b>type</b>:{String} 事件类型,
  412. * <br />"<b>point</b>:{Point} searchInfoWindow的打开位置}
  413. *
  414. * @example <b>参考示例:</b>
  415. * searchInfoWindow.addEventListener("open", function(e) {
  416. * alert(e.type);
  417. * });
  418. */
  419. /**
  420. * 关闭searchInfoWindow时,派发事件的接口
  421. * @name SearchInfoWindow#Close
  422. * @event
  423. * @param {Event Object} e 回调函数会返回event参数,包括以下返回值:
  424. * <br />{"<b>target</b> : {BMap.Overlay} 触发事件的元素,
  425. * <br />"<b>type</b>:{String} 事件类型,
  426. * <br />"<b>point</b>:{Point} searchInfoWindow的关闭位置}
  427. *
  428. * @example <b>参考示例:</b>
  429. * searchInfoWindow.addEventListener("close", function(e) {
  430. * alert(e.type);
  431. * });
  432. */
  433. /**
  434. * 启用自动平移
  435. * @return none
  436. *
  437. * @example <b>参考示例:</b><br />
  438. * searchInfoWindow.enableAutoPan();
  439. */
  440. SearchInfoWindow.prototype.enableAutoPan = function(){
  441. this._opts.enableAutoPan = true;
  442. }
  443. /**
  444. * 禁用自动平移
  445. * @return none
  446. *
  447. * @example <b>参考示例:</b><br />
  448. * searchInfoWindow.disableAutoPan();
  449. */
  450. SearchInfoWindow.prototype.disableAutoPan = function(){
  451. this._opts.enableAutoPan = false;
  452. }
  453. /**
  454. * 设置searchInfoWindow的内容
  455. * @param {String|HTMLElement} content 弹出气泡中的内容,支持HTML格式
  456. * @return none
  457. *
  458. * @example <b>参考示例:</b><br />
  459. * searchInfoWindow.setContent("百度地图API");
  460. */
  461. SearchInfoWindow.prototype.setContent = function(content){
  462. this._setContent(content);
  463. this._getSearchInfoWindowSize();
  464. this._adjustPosition(this._point);
  465. },
  466. /**
  467. * 设置searchInfoWindow的内容
  468. * @param {String|HTMLElement} title 弹出气泡中的内容,支持HTML格式
  469. * @return none
  470. *
  471. * @example <b>参考示例:</b><br />
  472. * searchInfoWindow.setTitle("百度地图API");
  473. */
  474. SearchInfoWindow.prototype.setTitle = function(title){
  475. this.dom.title.innerHTML = title;
  476. this._opts._title = title;
  477. }
  478. /**
  479. * 获取searchInfoWindow的内容
  480. * @return {String} String
  481. *
  482. * @example <b>参考示例:</b><br />
  483. * alret(searchInfoWindow.getContent());
  484. */
  485. SearchInfoWindow.prototype.getContent = function(){
  486. return this.dom.content.innerHTML;
  487. },
  488. /**
  489. * 获取searchInfoWindow的标题
  490. * @return {String} String
  491. *
  492. * @example <b>参考示例:</b><br />
  493. * alert(searchInfoWindow.getTitle());
  494. */
  495. SearchInfoWindow.prototype.getTitle = function(){
  496. return this.dom.title.innerHTML;
  497. }
  498. /**
  499. * 设置信息窗的地理位置
  500. * @param {Point} point 设置position
  501. * @return none
  502. *
  503. * @example <b>参考示例:</b><br />
  504. * searchInfoWindow.setPosition(new BMap.Point(116.35,39.911));
  505. */
  506. SearchInfoWindow.prototype.setPosition = function(poi){
  507. this._point = poi;
  508. this._adjustPosition(poi);
  509. this._panBox();
  510. this._removeMarkerEvt();
  511. }
  512. /**
  513. * 获得信息窗的地理位置
  514. * @return {Point} 信息窗的地理坐标
  515. *
  516. * @example <b>参考示例:</b><br />
  517. * searchInfoWindow.getPosition();
  518. */
  519. SearchInfoWindow.prototype.getPosition = function(){
  520. return this._point;
  521. }
  522. /**
  523. * 返回信息窗口的箭头距离信息窗口在地图
  524. * 上所锚定的地理坐标点的像素偏移量。
  525. * @return {Size} Size
  526. *
  527. * @example <b>参考示例:</b><br />
  528. * searchInfoWindow.getOffset();
  529. */
  530. SearchInfoWindow.prototype.getOffset = function(){
  531. return this._opts.offset;
  532. },
  533. baidu.object.extend(SearchInfoWindow.prototype,{
  534. /**
  535. * 保持屏幕只有一个searchInfoWindow,关闭现在已打开的searchInfoWindow
  536. */
  537. _closeOtherSearchInfo: function () {
  538. var instance = BMapLib.SearchInfoWindow.instance,
  539. len = instance.length;
  540. while (len--) {
  541. if (instance[len]._isOpen) {
  542. instance[len].close();
  543. }
  544. }
  545. },
  546. /**
  547. * 设置searchInfoWindow的内容
  548. * @param {String|HTMLElement} content 弹出气泡中的内容
  549. * @return none
  550. *
  551. * @example <b>参考示例:</b><br />
  552. * searchInfoWindow.setContent("百度地图API");
  553. */
  554. _setContent: function(content){
  555. if(!this.dom || !this.dom.content){
  556. return;
  557. }
  558. //string类型的content
  559. if(typeof content.nodeType === "undefined"){
  560. this.dom.content.innerHTML = content;
  561. }else{
  562. this.dom.content.appendChild(content);
  563. }
  564. var me = this;
  565. me._adjustContainerWidth();
  566. this._content = content;
  567. },
  568. /**
  569. * 调整searchInfoWindow的position
  570. * @return none
  571. */
  572. _adjustPosition: function(poi){
  573. var pixel = this._getPointPosition(poi);
  574. var icon = this._marker && this._marker.getIcon();
  575. if(this._marker){
  576. this.container.style.bottom = -(pixel.y - this._opts.offset.height - icon.anchor.height + icon.infoWindowAnchor.height) - this._marker.getOffset().height + 2 + 30 + "px";
  577. this.container.style.left = pixel.x - icon.anchor.width + this._marker.getOffset().width + icon.infoWindowAnchor.width - this._boxWidth / 2 + 28 + "px";
  578. }else{
  579. this.container.style.bottom = -(pixel.y - this._opts.offset.height) + 30 + "px";
  580. this.container.style.left = pixel.x - this._boxWidth / 2 + 25 + "px";
  581. }
  582. },
  583. /**
  584. * 得到searchInfoWindow的position
  585. * @return Point searchInfoWindow当前的position
  586. */
  587. _getPointPosition: function(poi){
  588. this._pointPosition = this._map.pointToOverlayPixel(poi);
  589. return this._pointPosition;
  590. },
  591. /**
  592. * 得到searchInfoWindow的高度跟宽度
  593. * @return none
  594. */
  595. _getSearchInfoWindowSize: function(){
  596. this._boxWidth = parseInt(this.container.offsetWidth,10);
  597. this._boxHeight = parseInt(this.container.offsetHeight,10);
  598. },
  599. /**
  600. * 阻止事件冒泡
  601. * @return none
  602. */
  603. _stopBubble: function(e){
  604. if(e && e.stopPropagation){
  605. e.stopPropagation();
  606. }else{
  607. window.event.cancelBubble = true;
  608. }
  609. },
  610. /**
  611. * 自动平移searchInfoWindow,使其在视野中全部显示
  612. * @return none
  613. */
  614. _panBox: function(){
  615. if(!this._opts.enableAutoPan){
  616. return;
  617. }
  618. var mapH = parseInt(this._map.getContainer().offsetHeight,10),
  619. mapW = parseInt(this._map.getContainer().offsetWidth,10),
  620. boxH = this._boxHeight,
  621. boxW = this._boxWidth;
  622. //searchInfoWindow窗口本身的宽度或者高度超过map container
  623. if(boxH >= mapH || boxW >= mapW){
  624. return;
  625. }
  626. //如果point不在可视区域内
  627. if(!this._map.getBounds().containsPoint(this._point)){
  628. this._map.setCenter(this._point);
  629. }
  630. var anchorPos = this._map.pointToPixel(this._point),
  631. panTop,panY,
  632. //左侧超出
  633. panLeft = boxW / 2 - 28 - anchorPos.x + 10,
  634. //右侧超出
  635. panRight = boxW / 2 + 28 + anchorPos.x - mapW + 10;
  636. if(this._marker){
  637. var icon = this._marker.getIcon();
  638. }
  639. //上侧超出
  640. var h = this._marker ? icon.anchor.height + this._marker.getOffset().height - icon.infoWindowAnchor.height : 0;
  641. panTop = boxH - anchorPos.y + this._opts.offset.height + h + 31 + 10;
  642. panX = panLeft > 0 ? panLeft : (panRight > 0 ? -panRight : 0);
  643. panY = panTop > 0 ? panTop : 0;
  644. this._map.panBy(panX,panY);
  645. },
  646. _removeMarkerEvt: function(){
  647. this._markerDragend && this._marker.removeEventListener("dragend", this._markerDragend);
  648. this._markerDragging && this._marker.removeEventListener("dragging", this._markerDragging);
  649. this._markerDragend = this._markerDragging = null;
  650. },
  651. /**
  652. * 集中派发事件函数
  653. *
  654. * @private
  655. * @param {Object} instance 派发事件的实例
  656. * @param {String} type 派发的事件名
  657. * @param {Json} opts 派发事件里添加的参数,可选
  658. */
  659. _dispatchEvent: function(instance, type, opts) {
  660. type.indexOf("on") != 0 && (type = "on" + type);
  661. var event = new baidu.lang.Event(type);
  662. if (!!opts) {
  663. for (var p in opts) {
  664. event[p] = opts[p];
  665. }
  666. }
  667. instance.dispatchEvent(event);
  668. },
  669. /**
  670. * 检索infowindow模板的初始化操作
  671. */
  672. _initSearchTemplate: function() {
  673. this._initDom();
  674. this._initPanelTemplate();
  675. this.setTitle(this._opts._title);
  676. if (this._opts.height) {
  677. this.dom.content.style.height = parseInt(this._opts.height, 10) + "px";
  678. }
  679. this._setContent(this._content);
  680. this._initService();
  681. this._bind();
  682. if (this._opts._searchTypes) {
  683. this._setSearchTypes();
  684. }
  685. this._mendIE6();
  686. },
  687. /**
  688. * 创建检索模板
  689. * @return dom
  690. */
  691. _createSearchTemplate: function() {
  692. if (!this._div) {
  693. var div = baidu.dom.create('div', {
  694. "class" : "BMapLib_SearchInfoWindow",
  695. "id" : "BMapLib_SearchInfoWindow" + this.guid
  696. });
  697. var template = [
  698. '<div class="BMapLib_bubble_top">',
  699. '<div class="BMapLib_bubble_title" id="BMapLib_bubble_title' + this.guid + '"></div>',
  700. '<div class="BMapLib_bubble_tools">',
  701. '<div class="BMapLib_bubble_close" title="关闭" id="BMapLib_bubble_close' + this.guid + '">',
  702. '</div>',
  703. '<div class="BMapLib_sendToPhone" title="发送到手机" id="BMapLib_sendToPhone' + this.guid + '">',
  704. '</div>',
  705. '</div>',
  706. '</div>',
  707. '<div class="BMapLib_bubble_center">',
  708. '<div class="BMapLib_bubble_content" id="BMapLib_bubble_content' + this.guid + '">',
  709. '</div>',
  710. '<div class="BMapLib_nav" id="BMapLib_nav' + this.guid + '">',
  711. '<ul class="BMapLib_nav_tab" id="BMapLib_nav_tab' + this.guid + '">', //tab选项卡
  712. '<li class="BMapLib_first BMapLib_current" id="BMapLib_tab_search' + this.guid + '" style="display:block;">',
  713. '<span class="BMapLib_icon BMapLib_icon_nbs"></span>在附近找',
  714. '</li>',
  715. '<li class="" id="BMapLib_tab_tohere' + this.guid + '" style="display:block;">',
  716. '<span class="BMapLib_icon BMapLib_icon_tohere"></span>到这里去',
  717. '</li>',
  718. '<li class="" id="BMapLib_tab_fromhere' + this.guid + '" style="display:block;">',
  719. '<span class="BMapLib_icon BMapLib_icon_fromhere"></span>从这里出发',
  720. '</li>',
  721. '</ul>',
  722. '<ul class="BMapLib_nav_tab_content">', //tab内容
  723. '<li id="BMapLib_searchBox' + this.guid + '">',
  724. '<table width="100%" align="center" border=0 cellpadding=0 cellspacing=0>',
  725. '<tr><td style="padding-left:8px;"><input id="BMapLib_search_text' + this.guid + '" class="BMapLib_search_text" type="text" maxlength="100" autocomplete="off"></td><td width="55" style="padding-left:7px;"><input id="BMapLib_search_nb_btn' + this.guid + '" type="submit" value="搜索" class="iw_bt"></td></tr>',
  726. '</table>',
  727. '</li>',
  728. '<li id="BMapLib_transBox' + this.guid + '" style="display:none">',
  729. '<table width="100%" align="center" border=0 cellpadding=0 cellspacing=0>',
  730. '<tr><td width="32" style="padding-left:5px;"><div id="BMapLib_stationText' + this.guid + '">起点</div></td><td><input id="BMapLib_trans_text' + this.guid + '" class="BMapLib_trans_text" type="text" maxlength="100" autocomplete="off"></td><td width="126" style="padding-left:7px;"><input id="BMapLib_search_bus_btn' + this.guid + '" type="button" value="公交" class="iw_bt" style="margin-right:5px;"><input id="BMapLib_search_drive_btn' + this.guid + '" type="button" class="iw_bt" value="驾车"></td></tr>',
  731. '</table>',
  732. '</li>',
  733. '</ul>',
  734. '</div>',
  735. '</div>',
  736. '<div class="BMapLib_bubble_bottom"></div>',
  737. '<img src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/iw_tail.png" width="58" height="31" alt="" class="BMapLib_trans" id="BMapLib_trans' + this.guid + '" style="left:144px;"/>'
  738. ];
  739. div.innerHTML = template.join("");
  740. this._div = div;
  741. }
  742. return this._div;
  743. },
  744. /**
  745. * 创建面板
  746. */
  747. _initPanelTemplate: function() {
  748. var panel = baidu.g(this._opts._panel);
  749. if (!this.dom.panel && panel) {
  750. panel.innerHTML = "";
  751. this.dom.panel = panel;
  752. //供地址选择页用的提示文字
  753. var address = baidu.dom.create('div');
  754. address.style.cssText = "display:none;background:#FD9;height:30px;line-height:30px;text-align:center;font-size:12px;color:#994C00;";
  755. panel.appendChild(address);
  756. this.dom.panel.address = address;
  757. //供地址检索面板用
  758. var localSearch = baidu.dom.create('div');
  759. panel.appendChild(localSearch);
  760. this.dom.panel.localSearch = localSearch;
  761. }
  762. },
  763. /**
  764. * 获取相关的DOM元素
  765. */
  766. _initDom: function () {
  767. if (!this.dom) {
  768. this.dom = {
  769. container : baidu.g("BMapLib_SearchInfoWindow" + this.guid) //容器
  770. , content : baidu.g("BMapLib_bubble_content" + this.guid) //主内容
  771. , title : baidu.g("BMapLib_bubble_title" + this.guid) //标题
  772. , closeBtn : baidu.g("BMapLib_bubble_close" + this.guid) //关闭按钮
  773. , transIco : baidu.g("BMapLib_trans" + this.guid) //infowindow底下三角形
  774. , navBox : baidu.g("BMapLib_nav" + this.guid) //检索容器
  775. , navTab : baidu.g("BMapLib_nav_tab" + this.guid) //tab容器
  776. , seartTab : baidu.g("BMapLib_tab_search" + this.guid) //在附近找tab
  777. , tohereTab : baidu.g("BMapLib_tab_tohere" + this.guid) //到这里去tab
  778. , fromhereTab : baidu.g("BMapLib_tab_fromhere" + this.guid) //从这里出发tab
  779. , searchBox : baidu.g("BMapLib_searchBox" + this.guid) //普通检索容器
  780. , transBox : baidu.g("BMapLib_transBox" + this.guid) //公交驾车检索容器,从这里出发和到这里去公用一个容器
  781. , stationText : baidu.g("BMapLib_stationText" + this.guid) //起点或终点文本
  782. , nbBtn : baidu.g("BMapLib_search_nb_btn" + this.guid) //普通检索按钮
  783. , busBtn : baidu.g("BMapLib_search_bus_btn" + this.guid) //公交驾车检索按钮
  784. , driveBtn : baidu.g("BMapLib_search_drive_btn" + this.guid) //驾车检索按钮
  785. , searchText : baidu.g("BMapLib_search_text" + this.guid) //普通检索文本框
  786. , transText : baidu.g("BMapLib_trans_text" + this.guid) //公交驾车检索文本框
  787. , sendToPhoneBtn : baidu.g("BMapLib_sendToPhone" + this.guid) //发送到手机
  788. }
  789. this.container = this.dom.container;
  790. }
  791. },
  792. /**
  793. * 设置infowindow内容的宽度
  794. */
  795. _adjustContainerWidth: function() {
  796. var width = 250,
  797. height = 0;
  798. if (this._opts.width) {
  799. width = parseInt(this._opts.width, 10);
  800. width += 10;
  801. } else {
  802. width = parseInt(this.dom.content.offsetWidth, 10);
  803. }
  804. if (width < 250) {
  805. width = 250;
  806. }
  807. this._width = width;
  808. //设置container的宽度
  809. this.container.style.width = this._width + "px";
  810. this._adjustTransPosition();
  811. },
  812. /**
  813. * 调整infowindow下三角形的位置
  814. */
  815. _adjustTransPosition: function() {
  816. //调整三角形的位置
  817. this.dom.transIco.style.left = this.container.offsetWidth / 2 - 2 - 29 + "px";
  818. this.dom.transIco.style.top = this.container.offsetHeight - 2 + "px";
  819. },
  820. /**
  821. * 初始化各检索服务
  822. */
  823. _initService: function () {
  824. var map = this._map;
  825. var me = this;
  826. var renderOptions = {}
  827. renderOptions.map = map;
  828. if (this.dom.panel) {
  829. renderOptions.panel = this.dom.panel.localSearch;
  830. }
  831. if (!this.localSearch) {
  832. this.localSearch = new BMap.LocalSearch(map, {
  833. renderOptions: renderOptions
  834. , onSearchComplete : function (result) {
  835. me._clearAddress();
  836. me._drawCircleBound();
  837. }
  838. });
  839. }
  840. if (!this.transitRoute) {
  841. this.transitRoute = new BMap.TransitRoute(map, {
  842. renderOptions: renderOptions
  843. , onSearchComplete : function (results) {
  844. me._transitRouteComplete(me.transitRoute, results);
  845. }
  846. });
  847. }
  848. if (!this.drivingRoute) {
  849. this.drivingRoute = new BMap.DrivingRoute(map, {
  850. renderOptions: renderOptions
  851. , onSearchComplete : function (results) {
  852. me._transitRouteComplete(me.drivingRoute, results);
  853. }
  854. });
  855. }
  856. },
  857. /**
  858. * 绑定事件
  859. */
  860. _bind: function () {
  861. var me = this;
  862. //关闭按钮
  863. baidu.on(this.dom.closeBtn, "click", function(e) {
  864. me.close();
  865. });
  866. baidu.on(this.dom.closeBtn, "touchstart", function(e) {
  867. me.close();
  868. });
  869. //发送到手机按钮
  870. baidu.on(this.dom.sendToPhoneBtn, "click", function(e) {
  871. me._sendToPhone();
  872. });
  873. baidu.on(this.dom.sendToPhoneBtn, "touchstart", function(e) {
  874. me._sendToPhone();
  875. });
  876. //周边检索tab键
  877. baidu.on(this.dom.seartTab, "click", function(e) {
  878. me._showTabContent(BMAPLIB_TAB_SEARCH);
  879. });
  880. baidu.on(this.dom.seartTab, "touchstart", function(e) {
  881. me._showTabContent(BMAPLIB_TAB_SEARCH);
  882. });
  883. //到这里去tab
  884. baidu.on(this.dom.tohereTab, "click", function(e) {
  885. me._showTabContent(BMAPLIB_TAB_TO_HERE);
  886. });
  887. baidu.on(this.dom.tohereTab, "touchstart", function(e) {
  888. me._showTabContent(BMAPLIB_TAB_TO_HERE);
  889. });
  890. //从这里出发tab
  891. baidu.on(this.dom.fromhereTab, "click", function(e) {
  892. me._showTabContent(BMAPLIB_TAB_FROM_HERE);
  893. });
  894. baidu.on(this.dom.fromhereTab, "touchstart", function(e) {
  895. me._showTabContent(BMAPLIB_TAB_FROM_HERE);
  896. });
  897. //周边检索输入框
  898. baidu.on(this.dom.searchText, "click", function(e) {
  899. me._localSearchAction();
  900. });
  901. baidu.on(this.dom.searchText, "touchstart", function(e) {
  902. me._localSearchAction();
  903. });
  904. //周边检索按钮
  905. baidu.on(this.dom.nbBtn, "click", function(e) {
  906. me._localSearchAction();
  907. });
  908. baidu.on(this.dom.nbBtn, "touchstart", function(e) {
  909. me._localSearchAction();
  910. });
  911. //公交检索按钮
  912. baidu.on(this.dom.busBtn, "click", function(e) {
  913. me._transitRouteAction(me.transitRoute);
  914. });
  915. baidu.on(this.dom.busBtn, "touchstart", function(e) {
  916. me._transitRouteAction(me.transitRoute);
  917. });
  918. //驾车检索按钮
  919. baidu.on(this.dom.driveBtn, "click", function(e) {
  920. me._transitRouteAction(me.drivingRoute);
  921. });
  922. baidu.on(this.dom.driveBtn, "touchstart", function(e) {
  923. me._transitRouteAction(me.drivingRoute);
  924. });
  925. //文本框自动完成提示
  926. this._autoCompleteIni();
  927. if (this._opts.enableSendToPhone === false) {
  928. this.dom.sendToPhoneBtn.style.display = 'none';
  929. }
  930. },
  931. /**
  932. * 显示tab内容
  933. */
  934. _showTabContent: function(type) {
  935. this._hideAutoComplete();
  936. var tabs = this.dom.navTab.getElementsByTagName("li"),
  937. len = tabs.length;
  938. for (var i = 0, len = tabs.length; i < len; i++) {
  939. tabs[i].className = "";
  940. }
  941. //显示当前tab content并高亮tab
  942. switch (type) {
  943. case BMAPLIB_TAB_SEARCH:
  944. this.dom.seartTab.className = "BMapLib_current";
  945. this.dom.searchBox.style.display = "block";
  946. this.dom.transBox.style.display = "none";
  947. break;
  948. case BMAPLIB_TAB_TO_HERE:
  949. this.dom.tohereTab.className = "BMapLib_current";
  950. this.dom.searchBox.style.display = "none";
  951. this.dom.transBox.style.display = "block";
  952. this.dom.stationText.innerHTML = "起点";
  953. this._pointType = "endPoint";
  954. break;
  955. case BMAPLIB_TAB_FROM_HERE:
  956. this.dom.fromhereTab.className = "BMapLib_current";
  957. this.dom.searchBox.style.display = "none";
  958. this.dom.transBox.style.display = "block";
  959. this.dom.stationText.innerHTML = "终点";
  960. this._pointType = "startPoint";
  961. break;
  962. }
  963. this._firstTab.className += " BMapLib_first";
  964. },
  965. /**
  966. * 绑定自动完成事件
  967. */
  968. _autoCompleteIni: function () {
  969. this.searchAC= new BMap.Autocomplete({
  970. "input" : this.dom.searchText
  971. , "location" : this._map
  972. });
  973. this.transAC = new BMap.Autocomplete({
  974. "input" : this.dom.transText
  975. , "location" : this._map
  976. });
  977. },
  978. /**
  979. * 关闭autocomplete
  980. */
  981. _hideAutoComplete: function () {
  982. this.searchAC.hide();
  983. this.transAC.hide();
  984. },
  985. /**
  986. * 销毁autoComplete对象
  987. */
  988. _disposeAutoComplete: function() {
  989. this.searchAC.dispose();
  990. this.transAC.dispose();
  991. },
  992. /**
  993. * 触发localsearch事件
  994. */
  995. _localSearchAction: function() {
  996. var kw = this._kw = this.dom.searchText.value;
  997. if (kw == "") {
  998. //检测是否为空
  999. this.dom.searchText.focus();
  1000. } else{
  1001. this._reset();
  1002. this.close();
  1003. var radius = this._radius = 1000;
  1004. this.localSearch.searchNearby(kw, this._point, radius);
  1005. }
  1006. },
  1007. /**
  1008. * 画周边检索的圆形圈
  1009. */
  1010. _drawCircleBound: function() {
  1011. this._closeCircleBound();
  1012. var circle = this._searchCircle = new BMap.Circle(this._point, this._radius, {
  1013. strokeWeight: 3,
  1014. strokeOpacity: 0.4,
  1015. strokeColor: "#e00",
  1016. filColor: "#00e",
  1017. fillOpacity:0.4
  1018. });
  1019. var label = this._searchLabel = new RadiusToolBar(this._point, this.guid);
  1020. this._map.addOverlay(circle);
  1021. this._map.addOverlay(label);
  1022. this._hasCircle = true;
  1023. },
  1024. /**
  1025. * 修改周边检索的半径
  1026. */
  1027. _changeSearchRadius: function() {
  1028. var radius = parseInt(baidu.g("BMapLib_search_radius" + this.guid).value, 10);
  1029. if (radius > 0 && radius != this._radius) {
  1030. this._radius = radius;
  1031. this.localSearch.searchNearby(this._kw, this._point, radius);
  1032. this._closeCircleBound();
  1033. }
  1034. },
  1035. /**
  1036. * 关闭周边检索的圆形圈
  1037. */
  1038. _closeCircleBound: function(radius) {
  1039. if (this._searchCircle) {
  1040. this._map.removeOverlay(this._searchCircle);
  1041. }
  1042. if (this._searchLabel) {
  1043. this._map.removeOverlay(this._searchLabel);
  1044. }
  1045. this._hasCircle = false;
  1046. },
  1047. /**
  1048. * 公交驾车检索查询
  1049. */
  1050. _transitRouteAction: function (transitDrive) {
  1051. var kw = this.dom.transText.value;
  1052. if (kw == "") {
  1053. //检测是否为空
  1054. this.dom.transText.focus();
  1055. } else {
  1056. this._reset();
  1057. this.close();
  1058. var transPoi = this._getTransPoi(kw);
  1059. transitDrive.search(transPoi.start, transPoi.end);
  1060. }
  1061. },
  1062. /**
  1063. * 公交驾车查询结束操作
  1064. */
  1065. _transitRouteComplete: function(transitDrive, results) {
  1066. this._clearAddress();
  1067. var status = transitDrive.getStatus();
  1068. //导航结果未知的情况
  1069. if (status == BMAP_STATUS_UNKNOWN_ROUTE) {
  1070. var startStatus = results.getStartStatus(),
  1071. endStatus = results.getEndStatus(),
  1072. tip = "";
  1073. tip = "找不到相关的线路";
  1074. if (startStatus == BMAP_ROUTE_STATUS_EMPTY && endStatus == BMAP_ROUTE_STATUS_EMPTY) {
  1075. tip = "找不到相关的起点和终点";
  1076. } else {
  1077. if (startStatus == BMAP_ROUTE_STATUS_EMPTY) {
  1078. tip = "找不到相关的起点";
  1079. }
  1080. if (endStatus == BMAP_ROUTE_STATUS_EMPTY) {
  1081. tip = "找不到相关的终点";
  1082. }
  1083. }
  1084. //当前搜索的点找不到明确的路线,但是可以检索到poi点
  1085. if (this._pointType == "startPoint" && endStatus == BMAP_ROUTE_STATUS_ADDRESS || this._pointType == "endPoint" && startStatus == BMAP_ROUTE_STATUS_ADDRESS) {
  1086. this._searchAddress(transitDrive);
  1087. } else {
  1088. this.dom.panel.address.style.display = "block";
  1089. this.dom.panel.address.innerHTML = tip;
  1090. }
  1091. }
  1092. },
  1093. /**
  1094. * 检索起点或终点的可选地址
  1095. */
  1096. _searchAddress: function(transitDrive) {
  1097. var me = this;
  1098. var panel = this.dom.panel;
  1099. if (!this.lsAddress) {
  1100. var renderOptions = {map: this._map};
  1101. if (panel) {
  1102. renderOptions.panel = this.dom.panel.localSearch;
  1103. }
  1104. this.lsAddress = new BMap.LocalSearch(this._map, {renderOptions: renderOptions});
  1105. }
  1106. var station = me._pointType == "startPoint" ? "终点" : "起点";
  1107. if (panel) {
  1108. this.dom.panel.address.style.display = "block";
  1109. this.dom.panel.address.innerHTML = "请选择准确的" + station;
  1110. }
  1111. this.lsAddress.setInfoHtmlSetCallback(function(poi,html){
  1112. var button = document.createElement('div');
  1113. button.style.cssText="position:relative;left:50%;margin:5px 0 0 -30px;width:60px;height:27px;line-height:27px;border:1px solid #E0C3A6;text-align:center;color:#B35900;cursor:pointer;background-color:#FFEECC;border-radius:2px; background-image: -webkit-gradient(linear, left top, left bottom, from(#FFFDF8), to(#FFEECC))";
  1114. button.innerHTML = '设为' + station;
  1115. html.appendChild(button);
  1116. baidu.on(button, "click", function(){
  1117. me._clearAddress();
  1118. var nowPoint = poi.marker.getPosition();
  1119. if (station == "起点") {
  1120. transitDrive.search(nowPoint, me._point);
  1121. } else {
  1122. transitDrive.search(me._point, nowPoint);
  1123. }
  1124. });
  1125. });
  1126. this._reset();
  1127. this.lsAddress.search(this.dom.transText.value);
  1128. },
  1129. /**
  1130. * 获取公交驾车的起终点
  1131. */
  1132. _getTransPoi: function(kw) {
  1133. var start, end;
  1134. if (this._pointType == "startPoint") {
  1135. start = this._point;
  1136. end = kw;
  1137. } else {
  1138. start = kw;
  1139. end = this._point;
  1140. }
  1141. return {
  1142. "start" : start,
  1143. "end" : end
  1144. }
  1145. },
  1146. /**
  1147. * 设置当前可提供的检索类型
  1148. */
  1149. _setSearchTypes: function () {
  1150. var searchTypes = this._unique(this._opts._searchTypes),
  1151. navTab = this.dom.navTab,
  1152. tabs = [this.dom.seartTab, this.dom.tohereTab, this.dom.fromhereTab],
  1153. i = 0,
  1154. len = 0,
  1155. curIndex = 0,
  1156. tab;
  1157. this.tabLength = searchTypes.length;
  1158. tabWidth = Math.floor((this._width - this.tabLength + 1) / this.tabLength);
  1159. if (searchTypes.length == 0) {
  1160. //若为空则不显示检索面板
  1161. this.dom.navBox.style.display = "none";
  1162. } else {
  1163. for (i = 0, len = tabs.length; i < len; i++) {
  1164. tabs[i].className = "";
  1165. tabs[i].style.display = "none";
  1166. }
  1167. for (i = 0; i < this.tabLength; i++) {
  1168. tab = tabs[searchTypes[i]];
  1169. if (i == 0) {
  1170. tab.className = "BMapLib_first BMapLib_current";
  1171. this._firstTab = tab;
  1172. curIndex = searchTypes[i];
  1173. }
  1174. if (i == this.tabLength - 1) {
  1175. //最后一个tab的宽度
  1176. var lastWidth = this._width - (this.tabLength - 1) * (tabWidth + 1);
  1177. if (baidu.browser.ie == 6) {
  1178. tab.style.width = lastWidth - 3 + "px";
  1179. } else {
  1180. tab.style.width = lastWidth + "px";
  1181. }
  1182. } else {
  1183. tab.style.width = tabWidth + "px";
  1184. }
  1185. tab.style.display = "block";
  1186. }
  1187. //按照数组顺序排序tab
  1188. if (searchTypes[1] != undefined) {
  1189. navTab.appendChild(tabs[searchTypes[1]])
  1190. }
  1191. if (searchTypes[2] != undefined) {
  1192. navTab.appendChild(tabs[searchTypes[2]])
  1193. }
  1194. this._showTabContent(curIndex);
  1195. }
  1196. this._adjustTransPosition();
  1197. },
  1198. /**
  1199. * 对用户提供的检索类型去重,并剔除无效的结果
  1200. */
  1201. _unique : function (arr) {
  1202. var len = arr.length,
  1203. result = arr.slice(0),
  1204. i,
  1205. datum;
  1206. // 从后往前双重循环比较
  1207. // 如果两个元素相同,删除后一个
  1208. while (--len >= 0) {
  1209. datum = result[len];
  1210. if (datum < 0 || datum > 2) {
  1211. result.splice(len, 1);
  1212. continue;
  1213. }
  1214. i = len;
  1215. while (i--) {
  1216. if (datum == result[i]) {
  1217. result.splice(len, 1);
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. return result;
  1223. },
  1224. /**
  1225. * 清除最近的结果
  1226. */
  1227. _reset : function () {
  1228. this.localSearch.clearResults();
  1229. this.transitRoute.clearResults();
  1230. this.drivingRoute.clearResults();
  1231. this._closeCircleBound();
  1232. this._hideAutoComplete();
  1233. },
  1234. /**
  1235. * 清除地址选择页结果
  1236. */
  1237. _clearAddress: function() {
  1238. if (this.lsAddress) {
  1239. this.lsAddress.clearResults();
  1240. }
  1241. if (this.dom.panel) {
  1242. this.dom.panel.address.style.display = "none";
  1243. }
  1244. },
  1245. /**
  1246. * IE6下处理PNG半透明
  1247. * @param {Object} infoWin
  1248. */
  1249. _mendIE6 : function(infoWin){
  1250. if(!baidu.browser.ie || baidu.browser.ie > 6){
  1251. return;
  1252. }
  1253. var popImg = this.container.getElementsByTagName("IMG");
  1254. for(var i = 0; i < popImg.length; i++) {
  1255. if (popImg[i].src.indexOf('.png') < 0) {
  1256. continue;
  1257. }
  1258. popImg[i].style.cssText += ';FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+ popImg[i].src +',sizingMethod=crop)'
  1259. popImg[i].src = "http://api.map.baidu.com/images/blank.gif";
  1260. }
  1261. },
  1262. /**
  1263. * 发送到手机
  1264. */
  1265. _sendToPhone : function(){
  1266. this.showPopup();
  1267. },
  1268. /**
  1269. * 弹出信息框
  1270. */
  1271. showPopup: function() {
  1272. if (!this.pop) {
  1273. this.pop = new Popup(this);
  1274. }
  1275. this._map.addOverlay(this.pop);
  1276. }
  1277. });
  1278. // 显示半径的自定义label
  1279. function RadiusToolBar(point, guid){
  1280. this._point = point;
  1281. this.guid = guid;
  1282. }
  1283. RadiusToolBar.prototype = new BMap.Overlay();
  1284. RadiusToolBar.prototype.initialize = function(map){
  1285. this._map = map;
  1286. var div = this._div = document.createElement("div");
  1287. function stopBubble(e){
  1288. if(e && e.stopPropagation){
  1289. e.stopPropagation();
  1290. }else{
  1291. window.event.cancelBubble = true;
  1292. }
  1293. }
  1294. baidu.on(div, 'mousedown', stopBubble);
  1295. baidu.on(div, 'touchstart', stopBubble);
  1296. baidu.on(div, 'click', stopBubble);
  1297. baidu.on(div, 'dblclick', stopBubble);
  1298. var searchInfoWindow = BMapLib.SearchInfoWindow.instance[this.guid];
  1299. div.style.cssText = 'position:absolute;white-space:nowrap;background:#fff;padding:1px;border:1px solid red;z-index:99;';
  1300. div.innerHTML = '<input type="text" value="' + searchInfoWindow._radius + '" style="width:30px;" id="BMapLib_search_radius' + this.guid + '"/>m <a href="javascript:void(0)" title="修改" onclick="BMapLib.SearchInfoWindow.instance[' + this.guid + ']._changeSearchRadius()" style="font-size:12px;text-decoration:none;color:blue;">修改</a><img src="http://api.map.baidu.com/images/iw_close1d3.gif" alt="关闭" title="关闭" style="cursor:pointer;padding:0 5px;" onclick="BMapLib.SearchInfoWindow.instance[' + this.guid + ']._closeCircleBound()"/>';
  1301. map.getPanes().labelPane.appendChild(div);
  1302. return div;
  1303. }
  1304. RadiusToolBar.prototype.draw = function(){
  1305. var map = this._map;
  1306. var pixel = map.pointToOverlayPixel(this._point);
  1307. this._div.style.left = pixel.x + 10 + "px";
  1308. this._div.style.top = pixel.y - 25 + "px";
  1309. }
  1310. //后端服务地址
  1311. var serviceHost = "http://api.map.baidu.com";
  1312. /**
  1313. * 创建弹层对象
  1314. */
  1315. function Popup(iw){
  1316. this.iw = iw;
  1317. }
  1318. Popup.prototype = new BMap.Overlay();
  1319. baidu.extend(Popup.prototype, {
  1320. initialize : function(map){
  1321. var me = this;
  1322. this._map = map;
  1323. this.container = this.generalDom();
  1324. this._map.getContainer().appendChild(this.container);
  1325. this.initDom();
  1326. this.bind();
  1327. this.getAddressByPoint();
  1328. this.getRememberPhone();
  1329. this.addPhoneNum = 0;
  1330. return this.container;
  1331. },
  1332. draw : function() {
  1333. },
  1334. generalDom: function() {
  1335. var dom = document.createElement('div'),
  1336. size = this._map.getSize(),
  1337. top = 0,
  1338. left = 0;
  1339. if (size.width > 450) {
  1340. left = (size.width - 450) / 2;
  1341. }
  1342. if (size.height > 260) {
  1343. top = (size.height - 260) / 2;
  1344. }
  1345. dom.style.cssText = "position:absolute;background:#fff;width:480px;height:260px;top:" + top + "px;left:" + left + "px;ovflow:hidden;";
  1346. var html = [
  1347. '<div class="BMapLib_sms_tab_container">',
  1348. '<span>发送到手机</span>',
  1349. '<span id="BMapLib_sms_tip" style="display:none;">',
  1350. '</span>',
  1351. '</div>',
  1352. '<div id="BMapLib_sms_pnl_phone" class="BMapLib_sms_pnl_phone" style="display: block;">',
  1353. '<div class="BMapLib_ap" id="pnl_phone_left" style="display: block;">',
  1354. '<table>',
  1355. '<tr>',
  1356. '<th>发送方手机号</th>',
  1357. '<td><input type="text" bid="" id="BMapLib_phone_0" maxlength="11" class="BMapLib_sms_input BMapLib_sms_input_l" /><span id="BMapLib_activateTip"></span></td>',
  1358. '</tr>',
  1359. '<tr id="BMapLib_activateBox" style="display:none;">',
  1360. '<th>激活码</th>',
  1361. '<td><input type="text" id="BMapLib_activate" class="BMapLib_sms_input BMapLib_sms_input_s" maxlength="4"/><input type="button" value="获取" id="BMapLib_activate_btn" bid="activate" />',
  1362. '</tr>',
  1363. '<tr>',
  1364. '<th></th>',
  1365. '<td>',
  1366. '</td>',
  1367. '</tr>',
  1368. '<tr>',
  1369. '<th style="vertical-align:top;padding-top:7px;">接收方手机号</th>',
  1370. '<td><div><input type="text" id="BMapLib_phone_1" class="BMapLib_sms_input BMapLib_sms_input_l" maxlength="11"/><input type="checkbox" id="BMapLib_is_remember_phone"/>记住此号</div>',
  1371. '<div id="BMapLib_add_phone_con"></div>',
  1372. '<div><a href="javascript:void(0)" id="BMapLib_add_phone_btn" bid="addPhone">新增</a></div>',
  1373. '</td>',
  1374. '</tr>',
  1375. '<tr>',
  1376. '<th></th>',
  1377. '<td ><input type="text" id="BMapLib_ver_input" maxlength="4" style="width:67px;border: 1px solid #a5acb2;vertical-align: middle;height:18px;" tabindex="5" placeholder="验证码"><img width="69" height="20" id="BMapLib_ver_image" bid="BMapLib_ver_image" style="border: 1px solid #d5d5d5;vertical-align:middle;margin-left: 5px;" alt="点击更换数字" title="点击更换数字"></td>',
  1378. '</tr>',
  1379. '<tr>',
  1380. '<th></th>',
  1381. '<td><input type="button" value="免费发送到手机" bid="sendToPhoneBtn"/></td>',
  1382. '</tr>',
  1383. '</table>',
  1384. '</div>',
  1385. '<div class="BMapLib_mp" id="pnl_phone_right" style="display: block;">',
  1386. '<div class="BMapLib_mp_title">短信内容:</div>',
  1387. '<div id="BMapLib_msgContent" class="BMapLib_msgContent"></div>',
  1388. '</div>',
  1389. '<div style="clear:both;"></div>',
  1390. '<p id="BMapLib_sms_declare_phone" class="BMapLib_sms_declare_phone">百度保证不向任何第三方提供输入的手机号码</p>',
  1391. '<div id="tipError" class="tip fail" style="display:none;">',
  1392. '<span id="tipText"></span>',
  1393. '<a href="javascript:void(0)" id="tipClose" class="cut"></a>',
  1394. '</div>',
  1395. '<div id="sms_lack_tip" style="display:none;">已达每日5次短信上限</div>',
  1396. '<div id="sms_unlogin_tip" style="display:none;">',
  1397. '<div style="padding-left:40px;">',
  1398. '<ul class="login_ul"><li id="normal_login-2" class="login_hover"><a href="javascript:void(0)">手机登录</a></li></ul>',
  1399. '<div id="loginBox_02" class="loginBox">',
  1400. '<div id="pass_error_info-2" class="pass_error_style"></div>',
  1401. '<div id="passports-2"></div>',
  1402. '<div id="loginIframe_iph-2" style="display:none"></div>',
  1403. '</div>',
  1404. '</div>',
  1405. '<div class="nopass" style="padding-left:128px;">没有百度帐号?<a href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=ma" target="_blank">立即注册</a></div>',
  1406. '</div>',
  1407. '</div>',
  1408. '<button class="BMapLib_popup_close" bid="close"></button>',
  1409. '<div id="BMapLib_success_tip" style="display:none;">您的短信已经发送到您的手机,请注意查收!</div>',
  1410. ].join('');
  1411. dom.innerHTML = html;
  1412. return dom;
  1413. },
  1414. initDom: function() {
  1415. this.dom = {
  1416. sms_tip : baidu.g('BMapLib_sms_tip'), //提示框
  1417. activate_btn : baidu.g('BMapLib_activate_btn'), //获取激活码按钮
  1418. fromphone : baidu.g("BMapLib_phone_0"),
  1419. tophone : baidu.g("BMapLib_phone_1"),
  1420. isRememberPhone : baidu.g("BMapLib_is_remember_phone"),
  1421. sms_container : baidu.g('BMapLib_sms_pnl_phone'),
  1422. success_tip : baidu.g('BMapLib_success_tip'), // 发送成功提示框
  1423. add_phone_con : baidu.g('BMapLib_add_phone_con'),
  1424. add_phone_btn : baidu.g('BMapLib_add_phone_btn'),
  1425. activateBox : baidu.g('BMapLib_activateBox'),
  1426. activateTip : baidu.g('BMapLib_activateTip'),
  1427. activate_input : baidu.g("BMapLib_activate"),
  1428. ver_image : baidu.g("BMapLib_ver_image"),
  1429. ver_input : baidu.g("BMapLib_ver_input")
  1430. }
  1431. },
  1432. showTip: function(result) {
  1433. var error = result.error;
  1434. var tipObj = {
  1435. 'PHONE_NUM_INVALID' : '手机号码无效',
  1436. 'SMS_SEND_SUCCESS' : '发送到手机成功',
  1437. 'AK_INVALID' : '你所使用的key无效',
  1438. 'INTERNAL_ERROR' : '服务器错误',
  1439. 'OVER_MAX_ACTIVATE_TIME' : '今天已超过发送激活码最大次数',
  1440. 'SMS_ACTIVATE_SUCCESS' : '激活码已发送到您的手机,请注意查收!',
  1441. 'ACTIVATE_FAIL' : '手机激活码无效',
  1442. 'SMS_LACK' : '今天您还能往5个手机发送短信',
  1443. 'PARAM_INVALID' : '请填完所有选项',
  1444. 'SEND_ACTIVATE_FAIL' : '激活码发送失败',
  1445. 'VCODE_VERITY_FAIL' : '验证码校验失败'
  1446. }
  1447. var tip = tipObj[error];
  1448. if (error == 'SMS_LACK') {
  1449. var res_sms = result.res_sms;
  1450. tip = "今天您还能往" + res_sms + "个手机发送短信";
  1451. this.addPhoneNum = res_sms - 1;
  1452. }
  1453. this.renderImageVer();
  1454. if (tip) {
  1455. this.dom.sms_tip.innerHTML = tip;
  1456. this.dom.sms_tip.style.display = "inline";
  1457. }
  1458. if (error == 'SMS_SEND_SUCCESS') {
  1459. this.rememberPhone();
  1460. this.sendSuccess();
  1461. }
  1462. },
  1463. //绑定事件
  1464. bind: function() {
  1465. var me = this;
  1466. me.renderImageVer();
  1467. baidu.on(this.container, 'click', function(e) {
  1468. var target = e.target || e.srcElement,
  1469. bid = target.getAttribute('bid');
  1470. switch (bid) {
  1471. case 'close':
  1472. me.closeActon();
  1473. break;
  1474. case 'sendToPhoneBtn':
  1475. me.sendAction();
  1476. break;
  1477. case 'activate':
  1478. me.activateAction();
  1479. break;
  1480. case 'addPhone':
  1481. me.addPhoneAction();
  1482. break;
  1483. case 'deletePhone':
  1484. me.deletePhoneAction(target);
  1485. break;
  1486. case 'BMapLib_ver_image':
  1487. me.renderImageVer();
  1488. }
  1489. });
  1490. var phone0 = baidu.g('BMapLib_phone_0');
  1491. var phone1 = baidu.g('BMapLib_phone_1');
  1492. //限制输入的字符,只能输数字和逗号
  1493. this.container.onkeypress = function (e) {
  1494. var event = e || window.e,
  1495. keyCode = event.which || event.keyCode,
  1496. result = false;
  1497. if (keyCode >= 48 && keyCode <= 57 || keyCode == 44 || keyCode == 8) {
  1498. result = true;
  1499. }
  1500. return result;
  1501. };
  1502. this.dom.ver_input.onkeypress = function(e){
  1503. me._stopBubble(e);
  1504. var event = e || window.e,
  1505. keyCode = event.which || event.keyCode,
  1506. result = false;
  1507. if (keyCode >= 48 && keyCode <= 57 ||keyCode >= 65 && keyCode <= 90||keyCode >= 97 && keyCode <= 122) {
  1508. result = true;
  1509. }
  1510. return result;
  1511. };
  1512. baidu.on(this.dom.fromphone, 'blur', function(){
  1513. if (baidu.isPhone(this.value)) {
  1514. me.checkActivateAction();
  1515. } else {
  1516. me.dom.activateTip.innerHTML = "";
  1517. me.dom.activateBox.style.display = "none";
  1518. }
  1519. });
  1520. baidu.on(this.dom.activate_input, 'blur', function(){
  1521. if (baidu.isActivateCode(this.value)) {
  1522. me.checkActivateAction();
  1523. }
  1524. });
  1525. },
  1526. /**
  1527. * 阻止事件冒泡
  1528. * @return none
  1529. */
  1530. _stopBubble: function(e){
  1531. if(e && e.stopPropagation){
  1532. e.stopPropagation();
  1533. }else{
  1534. window.event.cancelBubble = true;
  1535. }
  1536. },
  1537. //请求验证码.
  1538. renderImageVer:function(){
  1539. var me = this;
  1540. //验证码想着绑定
  1541. this.request("http://map.baidu.com/maps/services/captcha?",{"cbName":"cb"},function(data){
  1542. me.vcode = data['content']['vcode'];
  1543. me.dom.ver_image.src = 'http://map.baidu.com/maps/services/captcha/image?vcode=' + me.vcode;
  1544. });
  1545. },
  1546. //验证手机号是否已经激活
  1547. checkActivateAction: function() {
  1548. var param = {
  1549. phone : this.dom.fromphone.value,
  1550. activate : this.dom.activate_input.value,
  1551. cbName : 'callback'
  1552. }
  1553. var me = this;
  1554. this.request(this.config.ckActivateURL, param, function(res){
  1555. if (!res || res.isactivate == false) {
  1556. //未激活
  1557. me.dom.activateBox.style.display = "table-row";
  1558. me.dom.activateTip.style.color = "red";
  1559. me.dom.activateTip.innerHTML = "未激活";
  1560. } else {
  1561. //已激活
  1562. me.dom.activateBox.style.display = "none";
  1563. me.dom.activateTip.style.color = "green";
  1564. me.dom.activateTip.innerHTML = "已激活";
  1565. }
  1566. });
  1567. },
  1568. //点击激活按钮事件
  1569. activateAction: function() {
  1570. var me = this;
  1571. var ak = this._map.getKey();
  1572. var params = {
  1573. phone: this.dom.fromphone.value,
  1574. ak : ak,
  1575. cbName: "callback"
  1576. }
  1577. if (baidu.isPhone(params.phone)) {
  1578. this.request(this.config.activateURL, params, function(result){
  1579. if (result) {
  1580. me.showTip(result);
  1581. }
  1582. });
  1583. } else {
  1584. this.showTip({
  1585. error: 'PHONE_NUM_INVALID'
  1586. });
  1587. }
  1588. },
  1589. //点击关闭按钮事件
  1590. closeActon: function() {
  1591. this._map.removeOverlay(this);
  1592. },
  1593. // 获取短信的内容
  1594. getMessage: function() {
  1595. },
  1596. // 免费发送到手机点击事件
  1597. sendAction: function() {
  1598. var me = this;
  1599. if (this.validate()) {
  1600. tophoneStr = baidu.g("BMapLib_phone_1").value;
  1601. var addPhones = this.dom.add_phone_con.getElementsByTagName('input');
  1602. for (var i = 0, len = addPhones.length; i < len; i++) {
  1603. if (baidu.isPhone(addPhones[i].value)) {
  1604. tophoneStr += ',' + addPhones[i].value;
  1605. } else {
  1606. this.showTip({
  1607. error: 'PHONE_NUM_INVALID'
  1608. });
  1609. return;
  1610. }
  1611. }
  1612. var ak = this._map.getKey();
  1613. var params = {
  1614. fromphone : baidu.g("BMapLib_phone_0").value, //发送方手机
  1615. tophone : tophoneStr, //发送到手机
  1616. ak : ak, //用户ak
  1617. activate : this.dom.activate_input.value, //激活码
  1618. code_input : this.dom.ver_input.value,
  1619. vcode : this.vcode,
  1620. content : baidu.g("BMapLib_phone_0").value + "分享一个位置给您," + this.messageContent,
  1621. cbName : 'callback'
  1622. };
  1623. this.request(this.config.sendURL, params, function(result){
  1624. if (result) {
  1625. me.showTip(result);
  1626. }
  1627. });
  1628. }
  1629. },
  1630. //检验数据格式是否正确
  1631. validate: function() {
  1632. var flag = true;
  1633. if (!(baidu.isPhone(this.dom.fromphone.value) && baidu.isPhone(this.dom.tophone.value))) {
  1634. flag = false;
  1635. this.showTip({
  1636. error: 'PHONE_NUM_INVALID'
  1637. });
  1638. };
  1639. return flag;
  1640. },
  1641. getAddressByPoint: function() {
  1642. var pt = this.iw._point,
  1643. me = this,
  1644. gc = new BMap.Geocoder();
  1645. gc.getLocation(pt, function(rs){
  1646. if (rs && rs.addressComponents) {
  1647. var addComp = rs.addressComponents;
  1648. me.address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
  1649. me.generalMessage();
  1650. }
  1651. });
  1652. },
  1653. generalMessage: function() {
  1654. var msgContent = baidu.g('BMapLib_msgContent');
  1655. var msg = "";
  1656. var iw = this.iw;
  1657. var point = iw.getPosition();
  1658. if (this.userPhone) {
  1659. msg += this.userPhone + "分享一个位置给您,";
  1660. }
  1661. if (iw.getTitle) {
  1662. msg += "名称为:" + iw.getTitle() + ",";
  1663. }
  1664. if (this.address) {
  1665. msg += "大致位置在" + this.address + ",";
  1666. }
  1667. var uri = "http://api.map.baidu.com/marker?location=" + point.lat + "," + point.lng + "&title=" + encodeURIComponent(iw.getTitle()) + "&content=" + encodeURIComponent(iw.getContent()) + "&output=html";
  1668. var params = {
  1669. url : encodeURIComponent(uri),
  1670. t : new Date().getTime(),
  1671. cbName : 'callback'
  1672. };
  1673. var me = this;
  1674. this.request(this.config.shortURL, params, function(result){
  1675. msg += "查看地图:" + result.url ? result.url : uri;
  1676. me.messageContent = msg;
  1677. msgContent.innerHTML = msg;
  1678. });
  1679. },
  1680. //记住手机号码
  1681. rememberPhone: function() {
  1682. if (this.dom.isRememberPhone.checked) {
  1683. var phone = this.dom.tophone.value;
  1684. baidu.cookie.set('BMapLib_phone', phone, {
  1685. path: '/',
  1686. expires: 30 * 24 * 60 * 60 * 1000
  1687. });
  1688. }
  1689. },
  1690. //获取记住的手机号码
  1691. getRememberPhone: function() {
  1692. var phone = baidu.cookie.get('BMapLib_phone');
  1693. if (phone) {
  1694. this.dom.tophone.value = phone;
  1695. this.dom.isRememberPhone.checked = true;
  1696. }
  1697. },
  1698. //发送成功后执行
  1699. sendSuccess: function() {
  1700. this.dom.sms_container.style.display = "none";
  1701. this.dom.success_tip.style.display = "block";
  1702. var me = this;
  1703. setTimeout(function(){
  1704. me._map.removeOverlay(me);
  1705. }, 1500);
  1706. },
  1707. //新增手机号事件
  1708. addPhoneAction: function() {
  1709. if (this.addPhoneNum >= 4) {
  1710. } else {
  1711. var div = document.createElement('div');
  1712. div.innerHTML = '<input type="text" class="BMapLib_sms_input BMapLib_sms_input_l" maxlength="11"/><a href="javascript:void(0);" style="margin-left:5px;" bid="deletePhone">删除</a>';
  1713. this.dom.add_phone_con.appendChild(div);
  1714. this.addPhoneNum++;
  1715. }
  1716. },
  1717. //删除一个手机号事件
  1718. deletePhoneAction: function(target){
  1719. target.parentNode.parentNode.removeChild(target.parentNode);
  1720. this.addPhoneNum--;
  1721. },
  1722. //jsonp 请求
  1723. request: function(url, params, cbk) {
  1724. // 生成随机数
  1725. var timeStamp = (Math.random() * 100000).toFixed(0);
  1726. // 全局回调函数
  1727. BMapLib["BMapLib_cbk" + timeStamp] = function(json){
  1728. cbk && cbk(json);
  1729. delete BMapLib["BMapLib_cbk" + timeStamp];
  1730. };
  1731. for (var item in params) {
  1732. if (item != "cbName") {
  1733. url += '&' + item + "=" + params[item];
  1734. }
  1735. }
  1736. var me = this;
  1737. url += "&" + params.cbName + "=BMapLib.BMapLib_cbk" + timeStamp;
  1738. scriptRequest(url);
  1739. },
  1740. config: {
  1741. sendURL : serviceHost + "/ws/message?method=send",
  1742. activateURL : serviceHost + "/ws/message?method=activate",
  1743. ckActivateURL : serviceHost + "/ws/message?method=ckActivate",
  1744. shortURL : "http://j.map.baidu.com/?"
  1745. }
  1746. });
  1747. /**
  1748. * script标签请求
  1749. * @param {String} url 请求脚本url
  1750. */
  1751. function scriptRequest(url){
  1752. var script = document.createElement("script");
  1753. script.setAttribute("type", "text/javascript");
  1754. script.setAttribute("src", url);
  1755. // 脚本加载完成后进行移除
  1756. if (script.addEventListener) {
  1757. script.addEventListener('load', function(e) {
  1758. var t = e.target || e.srcElement;
  1759. t.parentNode.removeChild(t);
  1760. }, false);
  1761. }
  1762. else if (script.attachEvent) {
  1763. script.attachEvent('onreadystatechange', function(e) {
  1764. var t = window.event.srcElement;
  1765. if (t && (t.readyState == 'loaded' || t.readyState == 'complete')) {
  1766. t.parentNode.removeChild(t);
  1767. }
  1768. });
  1769. }
  1770. // 使用setTimeout解决ie6无法发送问题
  1771. setTimeout(function() {
  1772. document.getElementsByTagName('head')[0].appendChild(script);
  1773. script = null;
  1774. }, 1);
  1775. }
  1776. //用来存储创建出来的实例
  1777. var guid = 0;
  1778. BMapLib.SearchInfoWindow.instance = [];
  1779. })();