baidu.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5. <title>地址选择器</title>
  6. <link rel="stylesheet" href="__CDN__/assets/css/frontend.min.css"/>
  7. <link rel="stylesheet" href="__CDN__/assets/libs/font-awesome/css/font-awesome.min.css"/>
  8. <style type="text/css">
  9. body {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #container {
  14. position: absolute;
  15. left: 0;
  16. top: 0;
  17. right: 0;
  18. bottom: 0;
  19. }
  20. .confirm {
  21. position: absolute;
  22. bottom: 30px;
  23. right: 4%;
  24. z-index: 99;
  25. height: 50px;
  26. width: 50px;
  27. line-height: 50px;
  28. font-size: 15px;
  29. text-align: center;
  30. background-color: white;
  31. background: #1ABC9C;
  32. color: white;
  33. border: none;
  34. cursor: pointer;
  35. border-radius: 50%;
  36. }
  37. .search {
  38. position: absolute;
  39. width: 400px;
  40. top: 0;
  41. left: 50%;
  42. padding: 5px;
  43. margin-left: -200px;
  44. }
  45. label.BMapLabel {
  46. max-width: inherit;
  47. padding: .75rem 1.25rem;
  48. margin-bottom: 1rem;
  49. background-color: white;
  50. width: auto;
  51. min-width: 22rem;
  52. border: none;
  53. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="search">
  59. <div class="input-group">
  60. <input type="text" id="place" name="q" class="form-control" placeholder="输入地点"/>
  61. <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
  62. <span class="input-group-btn">
  63. <button type="button" name="search" id="address" class="btn btn-success">
  64. <i class="fa fa-search"></i>
  65. </button>
  66. </span>
  67. </div>
  68. </div>
  69. <div class="confirm">确定</div>
  70. <div id="container"></div>
  71. <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak={$config.baidukey|default=''}"></script>
  72. <script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  73. <script type="text/javascript">
  74. $(function () {
  75. // 百度地图API功能
  76. function G(id) {
  77. return document.getElementById(id);
  78. }
  79. var map, marker, searchService, address = null, lng, lat;
  80. var init = function () {
  81. map = new BMap.Map("container"); // 创建地图实例
  82. var point = new BMap.Point({$lng}, {$lat}); // 创建点坐标
  83. map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  84. map.centerAndZoom(point, parseInt("{$config.zoom}")); // 初始化地图,设置中心点坐标和地图级别
  85. var size = new BMap.Size(10, 20);
  86. map.addControl(new BMap.CityListControl({
  87. anchor: BMAP_ANCHOR_TOP_LEFT,
  88. offset: size,
  89. }));
  90. var geoc = new BMap.Geocoder();
  91. var addpoint = function (point) {
  92. //通过点击百度地图,可以获取到对应的point, 由point的lng、lat属性就可以获取对应的经度纬度
  93. var pt = point;
  94. geoc.getLocation(pt, function (rs) {
  95. //对象可以获取到详细的地址信息
  96. address = rs.address;
  97. deletePoint();
  98. var mk = new BMap.Marker(pt);
  99. map.addOverlay(mk);
  100. map.panTo(pt);
  101. var label = new BMap.Label('<div class="info">地址:' + address + '<br>经度:' + pt.lng + '<br>纬度:' + pt.lat + '</div>', {offset: new BMap.Size(16, 20)});
  102. label.setStyle({
  103. border: 'none',
  104. padding: '.75rem 1.25rem'
  105. });
  106. mk.setLabel(label);
  107. //将对应的HTML元素设置值
  108. lng = pt.lng;
  109. lat = pt.lat;
  110. });
  111. };
  112. if ("{$lng}" != '' && "{$lat}" != '') {
  113. addpoint(point);
  114. }
  115. ac = new BMap.Autocomplete({"input": "place", "location": map}); //建立一个自动完成的对象
  116. ac.addEventListener("onhighlight", function (e) { //鼠标放在下拉列表上的事件
  117. var str = "";
  118. var _value = e.fromitem.value;
  119. var value = "";
  120. if (e.fromitem.index > -1) {
  121. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  122. }
  123. str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
  124. value = "";
  125. if (e.toitem.index > -1) {
  126. _value = e.toitem.value;
  127. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  128. }
  129. str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
  130. G("searchResultPanel").innerHTML = str;
  131. });
  132. ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
  133. var _value = e.item.value;
  134. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  135. G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
  136. setPlace();
  137. });
  138. function setPlace() {
  139. map.clearOverlays(); //清除地图上所有覆盖物
  140. function myFun() {
  141. var result = local.getResults().getPoi(0);
  142. var pp = result.point; //获取第一个智能搜索的结果
  143. map.centerAndZoom(pp, 18);
  144. map.addOverlay(new BMap.Marker(pp)); //添加标注
  145. lng = pp.lng;
  146. lat = pp.lat;
  147. address = result.address;
  148. }
  149. var local = new BMap.LocalSearch(map, { //智能搜索
  150. onSearchComplete: myFun
  151. });
  152. local.search(myValue);
  153. }
  154. map.addEventListener("click", function (e) {
  155. //通过点击百度地图,可以获取到对应的point, 由point的lng、lat属性就可以获取对应的经度纬度
  156. var pt = e.point;
  157. addpoint(e.point);
  158. });
  159. /**
  160. * 清除覆盖物
  161. */
  162. function deletePoint() {
  163. var allOverlay = map.getOverlays();
  164. for (var i = 0; i < allOverlay.length; i++) {
  165. map.removeOverlay(allOverlay[i]);
  166. }
  167. }
  168. };
  169. var close = function (data) {
  170. var index = parent.Layer.getFrameIndex(window.name);
  171. var callback = parent.$("#layui-layer" + index).data("callback");
  172. //再执行关闭
  173. parent.Layer.close(index);
  174. //再调用回传函数
  175. if (typeof callback === 'function') {
  176. callback.call(undefined, data);
  177. }
  178. };
  179. //点击确定后执行回调赋值
  180. $(document).on('click', '.confirm', function () {
  181. var zoom = map.getZoom();
  182. var data = {lat: lat, lng: lng, zoom: zoom, address: address};
  183. close(data);
  184. });
  185. init();
  186. });
  187. </script>
  188. </body>
  189. </html>