amap.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. .amap-marker-label {
  46. border: 0;
  47. background-color: transparent;
  48. }
  49. .info {
  50. padding: .75rem 1.25rem;
  51. margin-bottom: 1rem;
  52. border-radius: .25rem;
  53. position: fixed;
  54. top: 2rem;
  55. background-color: white;
  56. width: auto;
  57. min-width: 22rem;
  58. border-width: 0;
  59. left: 1.8rem;
  60. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="search">
  66. <div class="input-group">
  67. <input type="text" id="place" name="q" class="form-control" placeholder="输入地点"/>
  68. <span class="input-group-btn">
  69. <button type="submit" name="search" id="search-btn" class="btn btn-success">
  70. <i class="fa fa-search"></i>
  71. </button>
  72. </span>
  73. </div>
  74. </div>
  75. <div class="confirm">确定</div>
  76. <div id="container"></div>
  77. <script type="text/javascript">
  78. window._AMapSecurityConfig = {
  79. securityJsCode: "{$config.amapsecurityjscode|default=''}",
  80. }
  81. </script>
  82. <script type="text/javascript" src="//webapi.amap.com/maps?v=1.4.11&key={$config.amapkey|default=''}&plugin=AMap.ToolBar,AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder"></script>
  83. <!-- UI组件库 1.0 -->
  84. <script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
  85. <script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  86. <script type="text/javascript">
  87. $(function () {
  88. var as, address, map, lat, lng, geocoder;
  89. var init = function () {
  90. AMapUI.loadUI(['misc/PositionPicker', 'misc/PoiPicker'], function (PositionPicker, PoiPicker) {
  91. //加载PositionPicker,loadUI的路径参数为模块名中 'ui/' 之后的部分
  92. map = new AMap.Map('container', {
  93. zoom: parseInt('{$config.zoom}')
  94. });
  95. geocoder = new AMap.Geocoder({
  96. radius: 1000 //范围,默认:500
  97. });
  98. var positionPicker = new PositionPicker({
  99. mode: 'dragMarker',//设定为拖拽地图模式,可选'dragMap'、'dragMarker',默认为'dragMap'
  100. map: map//依赖地图对象
  101. });
  102. //输入提示
  103. var autoOptions = {
  104. input: "place"
  105. };
  106. var relocation = function (lnglat) {
  107. lng = lnglat.lng;
  108. lat = lnglat.lat;
  109. map.panTo([lng, lat]);
  110. positionPicker.start(lnglat);
  111. geocoder.getAddress(lng + ',' + lat, function (status, result) {
  112. if (status === 'complete' && result.regeocode) {
  113. var address = result.regeocode.formattedAddress;
  114. var label = '<div class="info">地址:' + address + '<br>经度:' + lng + '<br>纬度:' + lat + '</div>';
  115. positionPicker.marker.setLabel({
  116. content: label //显示内容
  117. });
  118. } else {
  119. console.log(JSON.stringify(result));
  120. }
  121. });
  122. };
  123. var auto = new AMap.Autocomplete(autoOptions);
  124. //构造地点查询类
  125. var placeSearch = new AMap.PlaceSearch({
  126. map: map
  127. });
  128. //注册监听,当选中某条记录时会触发
  129. AMap.event.addListener(auto, "select", function (e) {
  130. placeSearch.setCity(e.poi.adcode);
  131. placeSearch.search(e.poi.name); //关键字查询查询
  132. });
  133. AMap.event.addListener(map, 'click', function (e) {
  134. relocation(e.lnglat);
  135. });
  136. //加载工具条
  137. var tool = new AMap.ToolBar();
  138. map.addControl(tool);
  139. var poiPicker = new PoiPicker({
  140. input: 'place',
  141. placeSearchOptions: {
  142. map: map,
  143. pageSize: 6 //关联搜索分页
  144. }
  145. });
  146. poiPicker.on('poiPicked', function (poiResult) {
  147. poiPicker.hideSearchResults();
  148. $('.poi .nearpoi').text(poiResult.item.name);
  149. $('.address .info').text(poiResult.item.address);
  150. $('#address').val(poiResult.item.address);
  151. $("#place").val(poiResult.item.name);
  152. relocation(poiResult.item.location);
  153. });
  154. positionPicker.on('success', function (positionResult) {
  155. as = positionResult.position;
  156. address = positionResult.address;
  157. lat = as.lat;
  158. lng = as.lng;
  159. });
  160. positionPicker.on('fail', function (positionResult) {
  161. address = '';
  162. });
  163. positionPicker.start();
  164. var defaultLng = {$lng}, defaultLat = {$lat};
  165. if (defaultLng && defaultLat) {
  166. relocation(new AMap.LngLat(Number(defaultLng), Number(defaultLat)));
  167. }
  168. });
  169. };
  170. //点击确定后执行回调赋值
  171. var close = function (data) {
  172. var index = parent.Layer.getFrameIndex(window.name);
  173. var callback = parent.$("#layui-layer" + index).data("callback");
  174. //再执行关闭
  175. parent.Layer.close(index);
  176. //再调用回传函数
  177. if (typeof callback === 'function') {
  178. callback.call(undefined, data);
  179. }
  180. };
  181. var getParam = function (name, url) {
  182. url = url || location.href;
  183. return url.match(new RegExp('[?&]' + name + '=([^?&]+)', 'i')) ? decodeURIComponent(RegExp.$1) : '';
  184. };
  185. //点击搜索按钮
  186. $(document).on('click', '.confirm', function () {
  187. var zoom = map.getZoom();
  188. var data = {lat: lat, lng: lng, zoom: zoom, address: address};
  189. close(data);
  190. });
  191. init();
  192. });
  193. </script>
  194. </body>
  195. </html>