map.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. (function () {
  2. var widgetName = 'map';
  3. UM.registerWidget(widgetName, {
  4. tpl: "<style type=\"text/css\">" +
  5. ".edui-dialog-map .edui-map-content{width:530px; height: 350px;margin: 10px auto;}" +
  6. ".edui-dialog-map .edui-map-content table{width: 100%}" +
  7. ".edui-dialog-map .edui-map-content table td{vertical-align: middle;}" +
  8. ".edui-dialog-map .edui-map-button { border: 1px solid #ccc; float: left; cursor: default; height: 23px; width: 70px; cursor: pointer; margin: 0; font-size: 12px; line-height: 24px; text-align: center; color:#444; }" +
  9. ".edui-dialog-map .edui-map-button:hover {background:#eee;}" +
  10. ".edui-dialog-map .edui-map-city,.edui-dialog-map .edui-map-address{height:21px;background: #FFF;border:1px solid #d7d7d7; line-height: 21px;}" +
  11. ".edui-dialog-map .edui-map-city{width:90px}" +
  12. ".edui-dialog-map .edui-map-address{width:150px}" +
  13. ".edui-dialog-map .edui-map-dynamic-label span{vertical-align:middle;margin: 3px 0px 3px 3px;}" +
  14. ".edui-dialog-map .edui-map-dynamic-label input{vertical-align:middle;margin: 3px;}" +
  15. "</style>" +
  16. "<div class=\"edui-map-content\">" +
  17. "<table>" +
  18. "<tr>" +
  19. "<td><%=lang_city%>:</td>" +
  20. "<td><input class=\"edui-map-city\" type=\"text\" value=\"<%=city.value%>\"/></td>" +
  21. "<td><%=lang_address%>:</td>" +
  22. "<td><input class=\"edui-map-address\" type=\"text\" value=\"\" /></td>" +
  23. "<td><a class=\"edui-map-button\"><%=lang_search%></a></td>" +
  24. "<td><label class=\"edui-map-dynamic-label\"><input class=\"edui-map-dynamic\" type=\"checkbox\" name=\"edui-map-dynamic\" /><span><%=lang_dynamicmap%></span></label></td>" +
  25. "</tr>" +
  26. "</table>" +
  27. "<div style=\"width:100%;height:340px;margin:5px auto;border:1px solid gray\" class=\"edui-map-container\"></div>" +
  28. "</div>" +
  29. "<script class=\"edui-tpl-container\" type=\"text/plain\">" +
  30. "<!DOCTYPE html>" +
  31. "<html>" +
  32. "<head>" +
  33. "<title></title>" +
  34. "</head>" +
  35. "<body>" +
  36. "<scr_ipt>" +
  37. "window.onload = function(){" +
  38. "var scripts = document.scripts || document.getElementsByTagName(\"script\")," +
  39. "src = [];" +
  40. "for( var i = 1, len = scripts.length; i<len; i++ ) {" +
  41. "src.push( scripts[i].src );" +
  42. "}" +
  43. "parent.UM.getEditor(<<id>>).getWidgetData(\'map\').requestMapApi( src );" +
  44. "};" +
  45. "function mapReadyStateChange ( state ) { " +
  46. " if ( state === 'complete' || state === 'loaded' ) {" +
  47. " document.close(); " +
  48. " } }" +
  49. "</scr_ipt>" +
  50. "<scr_ipt onreadystatechange='mapReadyStateChange(this.readyState);' onload='mapReadyStateChange(\"loaded\");' src=\"//api.map.baidu.com/api?v=2.0&ak=<%=key%>&services=true\"></scr_ipt>" +
  51. "</body>" +
  52. "</html>" +
  53. "</script>",
  54. initContent: function (editor, $widget) {
  55. if (!editor.options.baiduMapKey) {
  56. setTimeout(function () {
  57. $widget.edui().hide();
  58. alert("请在配置中配置百度地图API密钥");
  59. }, 10);
  60. return;
  61. }
  62. var me = this,
  63. lang = editor.getLang(widgetName),
  64. theme_url = editor.options.themePath + editor.options.theme;
  65. if (me.inited) {
  66. me.map.clearOverlays();
  67. var img = $(me.editor.selection.getRange().getClosedNode());
  68. if (img.length && /api[.]map[.]baidu[.]com/ig.test(img.attr("src"))) {
  69. var url = img.attr("src"),
  70. centerPos = me.getPars("center", url).split(","),
  71. markerPos = me.getPars("markers", url).split(",");
  72. var zoom = Number(me.getPars('zoom', url) || 11);
  73. var point = new BMap.Point(Number(centerPos[0]), Number(centerPos[1]));
  74. if (markerPos.length == 2) {
  75. setTimeout(function () {
  76. var marker = new BMap.Marker(new BMap.Point(Number(markerPos[0]), Number(markerPos[1])));
  77. marker.enableDragging();
  78. me.map.setCenter(point, zoom);
  79. me.map.addOverlay(marker);
  80. }, 100);
  81. }
  82. me.preventDefault();
  83. return false;
  84. } else {
  85. me.preventDefault();
  86. return false;
  87. }
  88. }
  89. me.inited = true;
  90. me.lang = lang;
  91. me.editor = editor;
  92. me.root().html($.parseTmpl(me.tpl, $.extend({}, lang['static'], {
  93. 'key': editor.options.baiduMapKey,
  94. 'theme_url': theme_url
  95. })));
  96. me.initRequestApi();
  97. },
  98. /**
  99. * 初始化请求API
  100. */
  101. initRequestApi: function () {
  102. var $ifr = null;
  103. //已经初始化过, 不用再次初始化
  104. if (window.BMap && window.BMap.Map) {
  105. this.initBaiduMap();
  106. } else {
  107. $ifr = $('<iframe style="display: none;"></iframe>');
  108. $ifr.appendTo(this.root());
  109. $ifr = $ifr[0].contentWindow.document;
  110. $ifr.open();
  111. $ifr.write(this.root().find(".edui-tpl-container").html().replace(/scr_ipt/g, 'script').replace('<<id>>', "'" + this.editor.id + "'"));
  112. }
  113. },
  114. requestMapApi: function (src) {
  115. var me = this;
  116. if (src.length) {
  117. var _src = src[0];
  118. src = src.slice(1);
  119. if (_src) {
  120. $.getScript(_src, function () {
  121. me.requestMapApi(src);
  122. });
  123. } else {
  124. me.requestMapApi(src);
  125. }
  126. } else {
  127. me.initBaiduMap();
  128. }
  129. },
  130. initBaiduMap: function () {
  131. var $root = this.root(),
  132. map = new BMap.Map($root.find(".edui-map-container")[0]),
  133. me = this,
  134. marker,
  135. point,
  136. imgcss,
  137. img = $(me.editor.selection.getRange().getClosedNode());
  138. map.enableInertialDragging();
  139. map.enableScrollWheelZoom();
  140. map.enableContinuousZoom();
  141. var url = '';
  142. if (img.length && /api[.]map[.]baidu[.]com/ig.test(img.attr("src"))) {
  143. url = img.attr("src");
  144. imgcss = img.attr('style');
  145. }
  146. var centerParam = me.getPars("center", url) || me.editor.options.baiduMapCenter || '116.404362,39.904768';
  147. var markerParam = me.getPars("markers", url) || '';
  148. var zoom = Number(me.getPars('zoom', url) || 11);
  149. var centerPos = centerParam.replace(/[\s]+/g, '').split(",");
  150. var markerPos = markerParam ? markerParam.replace(/[\s]+/g, '').split(",") : [];
  151. point = new BMap.Point(Number(centerPos[0]), Number(centerPos[1]));
  152. map.addControl(new BMap.NavigationControl());
  153. map.centerAndZoom(point, zoom);
  154. if (markerPos.length == 2) {
  155. marker = new BMap.Marker(new BMap.Point(Number(markerPos[0]), Number(markerPos[1])));
  156. marker.enableDragging();
  157. map.addOverlay(marker);
  158. }
  159. map.addEventListener('click', function (e, type, target, point, overlay) {
  160. map.clearOverlays();
  161. me.marker = new BMap.Marker(e.point);
  162. map.addOverlay(me.marker);
  163. });
  164. me.map = map;
  165. me.marker = marker;
  166. me.imgcss = imgcss;
  167. },
  168. doSearch: function () {
  169. var me = this,
  170. city = me.root().find('.edui-map-city').val(),
  171. address = me.root().find('.edui-map-address').val();
  172. if (!city) {
  173. alert(me.lang.cityMsg);
  174. return;
  175. }
  176. var search = new BMap.LocalSearch(city, {
  177. onSearchComplete: function (results) {
  178. if (results && results.getNumPois()) {
  179. var points = [];
  180. for (var i = 0; i < results.getCurrentNumPois(); i++) {
  181. points.push(results.getPoi(i).point);
  182. }
  183. if (points.length > 1) {
  184. me.map.setViewport(points);
  185. } else {
  186. me.map.centerAndZoom(points[0], 11);
  187. }
  188. point = me.map.getCenter();
  189. } else {
  190. alert(me.lang.errorMsg);
  191. }
  192. }
  193. });
  194. search.search(address || city);
  195. },
  196. getPars: function (name, url) {
  197. url = url || location.href;
  198. return url.match(new RegExp('[?&]' + name + '=([^?&]+)', 'i')) ? decodeURIComponent(RegExp.$1) : '';
  199. },
  200. reset: function () {
  201. this.map && this.map.reset();
  202. },
  203. initEvent: function () {
  204. var me = this,
  205. $root = me.root();
  206. $root.find('.edui-map-address').on('keydown', function (evt) {
  207. evt = evt || event;
  208. if (evt.keyCode == 13) {
  209. me.doSearch();
  210. return false;
  211. }
  212. });
  213. $root.find(".edui-map-button").on('click', function (evt) {
  214. me.doSearch();
  215. });
  216. $root.find(".edui-map-address").focus();
  217. $root.on("mousewheel DOMMouseScroll", function (e) {
  218. return false;
  219. });
  220. },
  221. width: 580,
  222. height: 408,
  223. buttons: {
  224. ok: {
  225. exec: function (editor) {
  226. var widget = editor.getWidgetData(widgetName),
  227. center = widget.map.getCenter(),
  228. zoom = widget.map.getZoom(),
  229. size = widget.map.getSize(),
  230. markerPoint = widget.marker ? widget.marker.point : null;
  231. if (widget.root().find(".edui-map-dynamic")[0].checked) {
  232. var URL = editor.getOpt('UMEDITOR_HOME_URL'),
  233. url = [URL + (/\/$/.test(URL) ? '' : '/') + "dialogs/map/map.html" +
  234. '?center=' + center.lng + ',' + center.lat,
  235. '&zoom=' + zoom,
  236. '&width=' + size.width,
  237. '&height=' + size.height,
  238. '&markers=' + (markerPoint ? markerPoint.lng + ',' + markerPoint.lat : '')].join('');
  239. editor.execCommand('inserthtml', '<iframe class="ueditor_baidumap" src="' + url + '" frameborder="0" width="' + (size.width + 4) + '" height="' + (size.height + 4) + '"></iframe>', true);
  240. } else {
  241. url = "https://api.map.baidu.com/staticimage?center=" + center.lng + ',' + center.lat +
  242. "&zoom=" + zoom + "&width=" + size.width + '&height=' + size.height + "&markers=" + (markerPoint ? markerPoint.lng + ',' + markerPoint.lat : '');
  243. editor.execCommand('inserthtml', '<img width="' + size.width + '" height="' + size.height + '" src="' + url + '"' + (widget.imgcss ? ' style="' + widget.imgcss + '"' : '') + '/>', true);
  244. }
  245. try {
  246. widget.reset();
  247. } catch (e) {
  248. }
  249. }
  250. },
  251. cancel: {
  252. exec: function (editor) {
  253. try {
  254. editor.getWidgetData(widgetName).reset();
  255. } catch (e) {
  256. }
  257. }
  258. }
  259. }
  260. }, function () {
  261. console.log(122);
  262. });
  263. })();