BaiduMap.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. MWF.xApplication.CRM = MWF.xApplication.CRM || {};
  2. MWF.xDesktop.requireApp("Template", "MForm", null, false);
  3. MWF.xDesktop.requireApp("CRM", "Template", null,false);
  4. MWF.require("MWF.widget.Identity", null,false);
  5. MWF.xApplication.CRM.BaiduMap = new Class({
  6. Extends: MWF.widget.Common,
  7. Implements: [Options, Events],
  8. options: {
  9. "style": "default"
  10. },
  11. initialize: function (node, app, explorer, actions, options) {
  12. this.setOptions(options);
  13. this.app = app;
  14. this.explorer = explorer;
  15. this.lp = app.lp.BaiduMap;
  16. this.path = "../x_component_CRM/$BaiduMap/";
  17. this.loadCss();
  18. this.actions = actions;
  19. this.node = $(node);
  20. },
  21. loadCss: function () {
  22. this.cssPath = "../x_component_CRM/$BaiduMap/" + this.options.style + "/css.wcss";
  23. this._loadCss();
  24. },
  25. load: function (markerData) {
  26. //this.node.empty();
  27. this.markerData = markerData;
  28. this.mapNode = new Element("div.mapNode", {styles : {
  29. width : "100%",
  30. height : "99%",
  31. "id":"mapMaxNode"
  32. }}).inject(this.node);
  33. setTimeout( function(){
  34. this.loadResource(function(){
  35. if(this.options.from = "newCustomer"){
  36. var _self = this;
  37. this.mapLocation = this.explorer.formTableArea.getElement("#mapLocation");
  38. this.mapLocation.set("disabled",false);
  39. this.mapLocation.setStyles({"background-color":"#ffffff"});
  40. this.mapLocation.addEvents({
  41. "keyup": function () {
  42. if(_self.explorer.adDiv)_self.explorer.adDiv.destroy();
  43. _self.explorer.adDiv = new Element("div.adDiv",{
  44. "styles":_self.explorer.css.adDiv
  45. }).inject(this.getParent());
  46. _self.explorer.adDiv.setStyles({
  47. "width":this.getWidth()+"px"
  48. });
  49. var v = this.get("value");
  50. if(v!=""){
  51. var myJSONP = new Request.JSONP({
  52. url: 'http://map.baidu.com/su?wd='+v,
  53. data: {
  54. "cid":"131",
  55. "type":"0",
  56. "from":"jsapi"
  57. },
  58. onRequest: function(url){
  59. // a script tag is created with a src attribute equal to url
  60. },
  61. onComplete: function(data){
  62. _self.resolve(data)
  63. }
  64. }).send();
  65. }else{
  66. _self.explorer.adDiv.destroy();
  67. _self.explorer.data.lat = "";
  68. _self.explorer.data.lng = ""
  69. }
  70. },
  71. "blur":function(){
  72. //if(_self.explorer.adDiv)_self.explorer.adDiv.hide();
  73. }
  74. })
  75. }
  76. }.bind(this));
  77. }.bind(this) , 100 )
  78. },
  79. loadMax: function (markerData) {
  80. this.markerData = markerData;
  81. this.maxMapDiv = new Element("div.maxMapDiv",{"styles":this.css.maxMapDiv}).inject(this.app.content);
  82. this.maxMapDiv.addEvents({
  83. "click":function(){
  84. this.maxMapDiv.destroy();
  85. }.bind(this)
  86. });
  87. this.mapHeadDiv = new Element("div.mapHeadDiv",{"styles":this.css.mapHeadDiv}).inject(this.maxMapDiv);
  88. this.maxCloseDiv = new Element("div.maxCloseDiv",{"styles":this.css.maxCloseDiv}).inject(this.mapHeadDiv);
  89. this.maxCloseDiv.addEvents({
  90. "mouseenter":function(){
  91. this.setStyles({
  92. "opacity":"1",
  93. "filter":"alpha(opacity=100)"
  94. })
  95. },
  96. "mouseleave":function(){
  97. this.maxCloseDiv.setStyles(this.css.maxCloseDiv);
  98. }.bind(this),
  99. "click":function(){
  100. //this.destroyForm();
  101. this.maxMapDiv.destroy();
  102. }.bind(this)
  103. });
  104. this.maxMapDiv.addEvents({
  105. "click":function(){
  106. this.maxMapDiv.destroy();
  107. }.bind(this)
  108. });
  109. this.mapContentDiv = new Element("div.mapContentDiv",{"styles":this.css.mapContentDiv}).inject(this.maxMapDiv);
  110. this.mapContentDiv.setStyles({
  111. "height":(this.app.content.getHeight()-this.mapHeadDiv.getHeight()-100)+"px",
  112. "width":(this.app.content.getWidth()-100)+"px"
  113. });
  114. this.mapContentDiv.addEvents({
  115. "click":function(e){
  116. e.stopPropagation();
  117. }
  118. });
  119. this.mapNode = new Element("div.mapNode", {styles : {
  120. width : "100%",
  121. height : "99%",
  122. "id":"mapMaxNode"
  123. }}).inject(this.mapContentDiv);
  124. setTimeout( function(){
  125. this.loadResource();
  126. }.bind(this) , 5 )
  127. },
  128. loadResource:function(callback){
  129. window.BMap_loadScriptTime = (new Date).getTime();
  130. //var apiPath = "http://api.map.baidu.com/api?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg";
  131. var apiPath = "http://api.map.baidu.com/getscript?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg&services=&t=20161219171637";
  132. if( !window.BDMapApiLoaded ){
  133. COMMON.AjaxModule.loadDom(apiPath, function () {
  134. window.BDMapApiLoaded = true;
  135. if( !window.BDMarkerToolLoaded ){
  136. COMMON.AjaxModule.load( "../x_component_CRM/BDMarkerTool.js", function(){
  137. window.BDMarkerToolLoaded = true;
  138. this._loadMap();
  139. if (callback)callback();
  140. }.bind(this) );
  141. }else{
  142. this._loadMap();
  143. if (callback)callback();
  144. }
  145. }.bind(this));
  146. }else{
  147. this._loadMap();
  148. if (callback)callback();
  149. }
  150. },
  151. _loadMap: function(){
  152. if(this.markerData){
  153. this.loadMap();
  154. }else{
  155. if (navigator.geolocation){
  156. try{
  157. navigator.geolocation.getCurrentPosition(this.loadMap.bind(this), this.loadMap.bind(this),{timeout:500});
  158. }catch( e ){
  159. this.loadMap();
  160. }
  161. }else{
  162. this.loadMap();
  163. }
  164. }
  165. },
  166. loadMap: function(position){
  167. this.createMap( position );
  168. },
  169. createMap: function( position ) {
  170. var point = null;
  171. if (this.markerData.longitude && this.markerData.latitude) {
  172. point = new BMap.Point(this.markerData.longitude, this.markerData.latitude);
  173. } else {
  174. if (position && position.coords) {
  175. point = new BMap.Point(position.coords.longitude, position.coords.latitude);
  176. }
  177. if (!point) {
  178. point = new BMap.Point(116.404, 39.915);
  179. }
  180. }
  181. this.map = new BMap.Map(this.mapNode); // 创建Map实例
  182. var marker = new BMap.Marker(point); // 创建标注
  183. this.map.addOverlay(marker); // 将标注添加到地图中
  184. this.map.centerAndZoom(point, 14); // 初始化地图,设置中心点坐标和地图级别
  185. this.map.panTo(point);
  186. // map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
  187. // map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
  188. this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  189. },
  190. resolve:function(data){
  191. var _self = this;
  192. if(data && data.s){
  193. data.s.each(function(d,i){
  194. //if(i>10) return false;
  195. var vs = d.split("$");
  196. var city = vs[0];
  197. var area = vs[1] || city;
  198. var ad = vs[3] || city;
  199. if(city!=""){
  200. var li = new Element("li.adLi",{
  201. "styles":this.explorer.css.adLi,
  202. "text":city+"-"+area+"-"+ad,
  203. "city":city
  204. }).inject(this.explorer.adDiv);
  205. li.addEvents({
  206. "click":function(e){
  207. var city = this.get("city");
  208. _self.map.clearOverlays(); //清除地图上所有覆盖物
  209. //_self.map.setCurrentCity(city);
  210. //_self.map.centerAndZoom(new BMap.Point(120.7, 30.7), 12); // 初始化地图,设置中心点坐标和地图级别
  211. var local = new BMap.LocalSearch(city, { //智能搜索
  212. onSearchComplete: function(){
  213. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  214. var province = local.getResults().province;
  215. var lat = pp.lat;
  216. var lng = pp.lng;
  217. var val = province==city?city+area+ad:province+city+area+ad;
  218. _self.mapLocation.set("value",val);
  219. _self.explorer.data.lat = lat;
  220. _self.explorer.data.lng = lng;
  221. _self.map.centerAndZoom(pp, 18);
  222. _self.map.addOverlay(new BMap.Marker(pp)); //添加标注
  223. _self.app.confirm("warn",e,_self.app.lp.confirm.customForm.replaceLocation.title,_self.app.lp.confirm.customForm.replaceLocation.content,300,120,function(){
  224. _self.explorer.formTableArea.getElement("#TProvinceValue").set("text",province);
  225. _self.explorer.formTableArea.getElement("#TCityValue").set("text",city);
  226. _self.explorer.formTableArea.getElement("#TAreaValue").set("text",area);
  227. //_self.explorer.form.getItem("TStreet").set("value",province==city?city+area+ad:province+city+area+ad);
  228. _self.explorer.form.getItem("TStreet").set("value",ad);
  229. _self.explorer.adDiv.destroy();
  230. this.close();
  231. },function(){
  232. this.close();
  233. _self.explorer.adDiv.destroy();
  234. });
  235. }
  236. });
  237. local.search(ad);
  238. },
  239. "mouseover":function(){
  240. this.setStyles({"background":"#999999","color":"#ffffff"});
  241. },
  242. "mouseout":function(){
  243. this.setStyles({"background":"#ffffff","color":""});
  244. }
  245. })
  246. }
  247. }.bind(this))
  248. }
  249. },
  250. setPlace:function(v){
  251. this.map.clearOverlays(); //清除地图上所有覆盖物
  252. var local = new BMap.LocalSearch(this.map, { //智能搜索
  253. onSearchComplete: function(){
  254. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  255. this.map.centerAndZoom(pp, 18);
  256. this.map.addOverlay(new BMap.Marker(pp)); //添加标注
  257. }.bind(this)
  258. });
  259. local.search(v);
  260. }
  261. });
  262. MWF.xApplication.CRM.BaiduMap.MaxMap = new Class({
  263. Extends: MWF.widget.Common,
  264. Implements: [Options, Events],
  265. options: {
  266. "style": "default",
  267. "width": "100%",
  268. "height": "100%"
  269. },
  270. initialize: function (explorer,app, actions, options) {
  271. this.setOptions(options);
  272. this.app = app;
  273. this.explorer = explorer;
  274. this.lp = app.lp.BaiduMap;
  275. this.path = "../x_component_CRM/$BaiduMap/";
  276. this.loadCss();
  277. this.actions = actions;
  278. },
  279. loadCss: function () {
  280. this.cssPath = "../x_component_CRM/$BaiduMap/" + this.options.style + "/css.wcss";
  281. this._loadCss();
  282. },
  283. load:function(data,callback){
  284. this.maxMapDiv = new Element("div.maxMapDiv",{"styles":this.css.maxMapDiv}).inject(this.app.content);
  285. this.mapHeadDiv = new Element("div.mapHeadDiv",{"styles":this.css.mapHeadDiv}).inject(this.maxMapDiv);
  286. this.mapContentDiv = new Element("div.mapContentDiv",{"styles":this.css.mapContentDiv}).inject(this.maxMapDiv);
  287. this.mapContentDiv.setStyles({
  288. "height":(this.app.content.getHeight()-this.mapHeadDiv.getHeight()-100)+"px",
  289. "width":(this.app.content.getWidth()-100)+"px"
  290. });
  291. setTimeout( function(){
  292. this.loadResource();
  293. }.bind(this) , 100 )
  294. },
  295. loadResource:function(callback){
  296. window.BMap_loadScriptTime = (new Date).getTime();
  297. var apiPath = "http://api.map.baidu.com/getscript?v=2.0&ak=Qac4WmBvHXiC87z3HjtRrbotCE3sC9Zg&services=&t=20161219171637";
  298. if( !window.BDMapApiLoaded ){
  299. COMMON.AjaxModule.loadDom(apiPath, function () {
  300. window.BDMapApiLoaded = true;
  301. if( !window.BDMarkerToolLoaded ){
  302. COMMON.AjaxModule.load( "../x_component_CRM/BDMarkerTool.js", function(){
  303. window.BDMarkerToolLoaded = true;
  304. this._loadMap();
  305. if (callback)callback();
  306. }.bind(this) );
  307. }else{
  308. this._loadMap();
  309. if (callback)callback();
  310. }
  311. }.bind(this));
  312. }else{
  313. this._loadMap();
  314. if (callback)callback();
  315. }
  316. },
  317. _loadMap: function(){
  318. if (navigator.geolocation){
  319. try{
  320. navigator.geolocation.getCurrentPosition(this.loadMap.bind(this), this.loadMap.bind(this));
  321. }catch( e ){
  322. this.loadMap();
  323. }
  324. }else{
  325. this.loadMap();
  326. }
  327. },
  328. loadMap: function(position){
  329. this.createMap( position );
  330. },
  331. createMap: function( position ) {
  332. var point = null;
  333. if (this.markerData) {
  334. point = new BMap.Point(this.markerData.longitude, this.markerData.latitude);
  335. } else {
  336. if (position && position.coords) {
  337. point = new BMap.Point(position.coords.longitude, position.coords.latitude);
  338. }
  339. if (!point) {
  340. point = new BMap.Point(116.404, 39.915);
  341. }
  342. }
  343. this.map = new BMap.Map(this.mapNode); // 创建Map实例
  344. var marker = new BMap.Marker(point); // 创建标注
  345. this.map.addOverlay(marker); // 将标注添加到地图中
  346. this.map.panTo(point);
  347. this.map.centerAndZoom(point, 12); // 初始化地图,设置中心点坐标和地图级别
  348. // map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点坐标和地图级别
  349. // map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
  350. this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  351. }
  352. });