maplocation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'cwmap/maplocation/index',
  8. add_url: 'cwmap/maplocation/add',
  9. edit_url: 'cwmap/maplocation/edit',
  10. del_url: 'cwmap/maplocation/del',
  11. table: 'map_location',
  12. }
  13. });
  14. var table = $("#table");
  15. // 初始化表格
  16. table.bootstrapTable({
  17. url: $.fn.bootstrapTable.defaults.extend.index_url,
  18. pk: 'id',
  19. sortName: 'id',
  20. columns: [
  21. [
  22. {checkbox: true},
  23. {field: 'id', title: __('Id'), visible: false, sortable: false},
  24. {field: 'locationname', title: __('Locationname')},
  25. {field: 'detailaddress', title: __('Detailaddress')},
  26. {field: 'longitude', title: __('Longitude'), visible: false, sortable: false},
  27. {field: 'latitude', title: __('Latitude'), visible: false, sortable: false},
  28. {field: 'phone', title: __('Phone')},
  29. {field: 'email', title: __('Email')},
  30. {field: 'fax', title: __('Fax')},
  31. {field: 'qq', title: __('Qq')},
  32. {field: 'website', title: __('Website')},
  33. {field: 'picture', title: __('Picture'), visible: false, sortable: false, formatter: Table.api.formatter.image},
  34. {field: 'province', title: __('Province')},
  35. {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
  36. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  37. ]
  38. ]
  39. });
  40. // 为表格绑定事件
  41. Table.api.bindevent(table);
  42. },
  43. add: function () {
  44. Controller.api.bindevent();
  45. },
  46. edit: function () {
  47. Controller.api.bindevent();
  48. },
  49. map: function () {
  50. Form.api.bindevent($("form[role=form]"));
  51. require(['async!BMap3'], function () {
  52. // 更多文档可参考 http://lbsyun.baidu.com/jsdemo.htm
  53. // 百度地图API功能
  54. var map = new BMap.Map("allmap");
  55. var point = new BMap.Point(116.404, 39.915);//精度,纬度
  56. map.centerAndZoom(point, 20); //设置中心坐标点和级别
  57. var marker = new BMap.Marker(point); // 创建标注
  58. map.addOverlay(marker); // 将标注添加到地图中
  59. marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  60. map.enableDragging(); //开启拖拽
  61. //map.enableInertialDragging(); //开启惯性拖拽
  62. map.enableScrollWheelZoom(true); //是否允许缩放
  63. //map.centerAndZoom("上海",15); //根据城市名设定地图中心点
  64. function G(id) {
  65. return document.getElementById(id);
  66. }
  67. var ac = new BMap.Autocomplete(//建立一个自动完成的对象
  68. {"input": "searchaddress"
  69. , "location": map
  70. });
  71. ac.addEventListener("onhighlight", function (e) { //鼠标放在下拉列表上的事件
  72. var str = "";
  73. var _value = e.fromitem.value;
  74. var value = "";
  75. if (e.fromitem.index > -1) {
  76. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  77. }
  78. str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
  79. value = "";
  80. if (e.toitem.index > -1) {
  81. _value = e.toitem.value;
  82. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  83. }
  84. str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
  85. G("searchResultPanel").innerHTML = str;
  86. });
  87. var myValue;
  88. ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
  89. var _value = e.item.value;
  90. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  91. G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
  92. setPlace();
  93. });
  94. function setPlace() {
  95. map.clearOverlays(); //清除地图上所有覆盖物
  96. function myFun() {
  97. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  98. map.centerAndZoom(pp, 18);
  99. map.addOverlay(new BMap.Marker(pp)); //添加标注
  100. }
  101. var local = new BMap.LocalSearch(map, {//智能搜索
  102. onSearchComplete: myFun
  103. });
  104. local.search(myValue);
  105. }
  106. //单击获取点击的经纬度
  107. var geoc = new BMap.Geocoder();
  108. map.addEventListener("click", function (e) {
  109. var pt = e.point;
  110. geoc.getLocation(pt, function (rs) {
  111. var addComp = rs.addressComponents;
  112. Layer.alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + " <br> "
  113. + __('Longitude') + ' : ' + e.point.lng + ' , ' + __('Latitude') + ' : ' + e.point.lat);
  114. // console.log(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + " <br> "
  115. // + __('Longitude') + ' : ' + e.point.lng + ' , ' + __('Latitude') + ' : ' + e.point.lat);
  116. });
  117. });
  118. //
  119. // // 点搜索按钮时解析地址坐标
  120. // $(document).on('click', '.btn-search', function () {
  121. // var local = new BMap.LocalSearch(map, {
  122. // renderOptions: {map: map}
  123. // });
  124. // var searchkeyword = $("#searchaddress").val();
  125. // local.search(searchkeyword);
  126. // });
  127. });
  128. },
  129. api: {
  130. bindevent: function () {
  131. Form.api.bindevent($("form[role=form]"));
  132. var default_zoom = 10;
  133. var find_zoom = 18;
  134. require(['async!BMap3'], function () {
  135. var longitude = $("#c-longitude").val();
  136. var latitude = $("#c-latitude").val();
  137. // 百度地图API功能
  138. var map = new BMap.Map("allmap");
  139. var point;
  140. if (longitude == "") {
  141. point = new BMap.Point(116.404, 39.915);
  142. map.centerAndZoom(point, default_zoom);
  143. } else {
  144. point = new BMap.Point(longitude, latitude);
  145. map.centerAndZoom(point, find_zoom);
  146. marker = new BMap.Marker(point); // 创建标注
  147. map.addOverlay(marker); // 将标注添加到地图中
  148. marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  149. }
  150. var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
  151. map.addControl(top_left_navigation);
  152. // 创建地址解析器实例
  153. var myGeo = new BMap.Geocoder();
  154. map.addEventListener("click", function (e) {
  155. var pt = e.point;
  156. myGeo.getLocation(pt, function (rs) {
  157. var addComp = rs.addressComponents;
  158. $("#c-longitude").val(pt.lng);
  159. $("#c-latitude").val(pt.lat);
  160. $("#c-province").val(addComp.province);
  161. Layer.msg(__('Position update') + ' <br> ' + __('Longitude') + ' : ' + pt.lng + ' , ' + __('Latitude') + ' : ' + pt.lat);
  162. map.clearOverlays();
  163. marker = new BMap.Marker(pt); // 创建标注
  164. map.addOverlay(marker); // 将标注添加到地图中
  165. marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  166. });
  167. });
  168. function G(id) {
  169. return document.getElementById(id);
  170. }
  171. var ac = new BMap.Autocomplete(//建立一个自动完成的对象
  172. {"input": "searchaddress"
  173. , "location": map
  174. });
  175. var myValue;
  176. ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
  177. var _value = e.item.value;
  178. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  179. G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
  180. setPlace();
  181. });
  182. function setPlace() {
  183. map.clearOverlays(); //清除地图上所有覆盖物
  184. function myFun() {
  185. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  186. myGeo.getLocation(pp, function (rs) {
  187. var addComp = rs.addressComponents;
  188. $("#c-longitude").val(pp.lng);
  189. $("#c-latitude").val(pp.lat);
  190. $("#c-province").val(addComp.province);
  191. map.centerAndZoom(pp, find_zoom);
  192. marker = new BMap.Marker(pp); // 创建标注
  193. map.addOverlay(marker); // 将标注添加到地图中
  194. marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  195. Layer.msg(__('Position update') + ' <br> ' + __('Longitude') + ' : ' + pp.lng + ' , ' + __('Latitude') + ' : ' + pp.lat);
  196. });
  197. }
  198. var local = new BMap.LocalSearch(map, {//智能搜索
  199. onSearchComplete: myFun
  200. });
  201. local.search(myValue);
  202. }
  203. })
  204. }
  205. }
  206. };
  207. return Controller;
  208. });