tencent.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. .autocomplete-search {
  46. text-align: left;
  47. cursor: default;
  48. background: #fff;
  49. border-radius: 2px;
  50. -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  51. -moz-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  52. box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  53. background-clip: padding-box;
  54. position: absolute;
  55. display: none;
  56. z-index: 1036;
  57. max-height: 254px;
  58. overflow: hidden;
  59. overflow-y: auto;
  60. box-sizing: border-box;
  61. }
  62. .autocomplete-search .autocomplete-suggestion {
  63. padding: 5px;
  64. }
  65. .autocomplete-search .autocomplete-suggestion:hover {
  66. background: #f0f0f0;
  67. }
  68. </style>
  69. </head>
  70. <body>
  71. <div class="search">
  72. <div class="input-group">
  73. <input type="text" id="place" name="q" class="form-control" placeholder="输入地点"/>
  74. <span class="input-group-btn">
  75. <button type="button" name="search" id="search-btn" class="btn btn-success">
  76. <i class="fa fa-search"></i>
  77. </button>
  78. </span>
  79. </div>
  80. </div>
  81. <div class="confirm">确定</div>
  82. <div id="container"></div>
  83. <script charset="utf-8" src="//map.qq.com/api/js?v=2.exp&libraries=place&key={$config.tencentkey|default=''}"></script>
  84. <script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  85. <script src="__CDN__/assets/addons/address/js/gcoord.min.js"></script>
  86. <script src="__CDN__/assets/addons/address/js/jquery.autocomplete.js"></script>
  87. <script type="text/javascript">
  88. $(function () {
  89. var map, marker, geocoder, infoWin, searchService, keyword, address, fromtype, totype;
  90. address = "{$address|htmlentities}";
  91. var lng = Number("{$lng}");
  92. var lat = Number("{$lat}");
  93. fromtype = "GCJ02";
  94. totype = "{$config.coordtype|default='DEFAULT'}"
  95. totype = totype === 'DEFAULT' ? "GCJ02" : totype;
  96. if (lng && lat && fromtype !== totype) {
  97. var result = gcoord.transform([lng, lat], gcoord[totype], gcoord[fromtype]);
  98. lng = result[0] || lng;
  99. lat = result[1] || lat;
  100. }
  101. var init = function () {
  102. var center = new qq.maps.LatLng(lat, lng);
  103. map = new qq.maps.Map(document.getElementById('container'), {
  104. center: center,
  105. zoom: parseInt("{$config.zoom}")
  106. });
  107. //实例化信息窗口
  108. infoWin = new qq.maps.InfoWindow({
  109. map: map
  110. });
  111. geocoder = {
  112. getAddress: function (latLng) {
  113. $.ajax({
  114. url: "https://apis.map.qq.com/ws/geocoder/v1/?location=" + latLng.lat + "," + latLng.lng + "&key={$config.tencentkey|default=''}&output=jsonp",
  115. dataType: "jsonp",
  116. type: 'GET',
  117. cache: true,
  118. crossDomain: true,
  119. success: function (ret) {
  120. console.log("getAddress:", ret)
  121. if (ret.status === 0) {
  122. var component = ret.result.address_component;
  123. if (ret.result.formatted_addresses && ret.result.formatted_addresses.recommend) {
  124. var recommend = ret.result.formatted_addresses.recommend;
  125. var standard_address = ret.result.formatted_addresses.standard_address;
  126. var address = component.province !== component.city ? component.province + component.city : component.province;
  127. address = address + (recommend.indexOf(component.district) === 0 ? '' : component.district) + recommend;
  128. } else {
  129. address = ret.result.address;
  130. }
  131. showMarker(ret.result.location, address);
  132. showInfoWin(ret.result.location, address);
  133. }
  134. },
  135. error: function (e) {
  136. console.log(e, 'error')
  137. }
  138. });
  139. }
  140. };
  141. //初始化marker
  142. showMarker(center);
  143. if (address) {
  144. showInfoWin(center, address);
  145. } else {
  146. geocoder.getAddress(center);
  147. }
  148. var place = $("#place");
  149. place.autoComplete({
  150. minChars: 1,
  151. cache: 0,
  152. menuClass: 'autocomplete-search',
  153. source: function (term, response) {
  154. try {
  155. xhr.abort();
  156. } catch (e) {
  157. }
  158. xhr = $.ajax({
  159. url: "https://apis.map.qq.com/ws/place/v1/suggestion?keyword=" + term + "&key={$config.tencentkey|default=''}&output=jsonp",
  160. dataType: "jsonp",
  161. type: 'GET',
  162. cache: true,
  163. success: function (ret) {
  164. if (ret.status === 0) {
  165. if(ret.data.length === 0){
  166. $(".autocomplete-suggestions.autocomplete-search").html('');
  167. }
  168. response(ret.data);
  169. } else {
  170. console.log(ret);
  171. }
  172. }
  173. });
  174. },
  175. renderItem: function (item, search) {
  176. search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  177. var regexp = new RegExp("(" + search.replace(/[\,|\u3000|\uff0c]/, ' ').split(' ').join('|') + ")", "gi");
  178. return "<div class='autocomplete-suggestion' data-item='" + JSON.stringify(item) + "' data-title='" + item.title + "' data-val='" + item.title + "'>" + item.title.replace(regexp, "<b>$1</b>") + "</div>";
  179. },
  180. onSelect: function (e, term, sel) {
  181. e.preventDefault();
  182. var item = $(sel).data("item");
  183. //调用获取位置方法
  184. geocoder.getAddress(item.location);
  185. var position = new qq.maps.LatLng(item.location.lat, item.location.lng);
  186. map.setCenter(position);
  187. }
  188. });
  189. //地图点击
  190. qq.maps.event.addListener(map, 'click', function (event) {
  191. try {
  192. //调用获取位置方法
  193. geocoder.getAddress(event.latLng);
  194. } catch (e) {
  195. console.log(e);
  196. }
  197. }
  198. );
  199. };
  200. //显示info窗口
  201. var showInfoWin = function (latLng, title) {
  202. var position = new qq.maps.LatLng(latLng.lat, latLng.lng);
  203. infoWin.open();
  204. infoWin.setContent(title);
  205. infoWin.setPosition(position);
  206. };
  207. //实例化marker和监听拖拽结束事件
  208. var showMarker = function (latLng, title) {
  209. console.log("showMarker", latLng, title)
  210. var position = new qq.maps.LatLng(latLng.lat, latLng.lng);
  211. marker && marker.setMap(null);
  212. marker = new qq.maps.Marker({
  213. map: map,
  214. position: position,
  215. draggable: true,
  216. title: title || '拖动图标选择位置'
  217. });
  218. //监听拖拽结束
  219. qq.maps.event.addListener(marker, 'dragend', function (event) {
  220. //调用获取位置方法
  221. geocoder.getAddress(event.latLng);
  222. });
  223. };
  224. var close = function (data) {
  225. var index = parent.Layer.getFrameIndex(window.name);
  226. var callback = parent.$("#layui-layer" + index).data("callback");
  227. //再执行关闭
  228. parent.Layer.close(index);
  229. //再调用回传函数
  230. if (typeof callback === 'function') {
  231. callback.call(undefined, data);
  232. }
  233. };
  234. //点击确定后执行回调赋值
  235. $(document).on('click', '.confirm', function () {
  236. var zoom = map.getZoom();
  237. var data = {lat: infoWin.position.lat.toFixed(5), lng: infoWin.position.lng.toFixed(5), zoom: zoom, address: infoWin.content};
  238. if (fromtype !== totype) {
  239. var result = gcoord.transform([data.lng, data.lat], gcoord[fromtype], gcoord[totype]);
  240. data.lng = (result[0] || data.lng).toFixed(5);
  241. data.lat = (result[1] || data.lat).toFixed(5);
  242. console.log(data, result, fromtype, totype);
  243. }
  244. close(data);
  245. });
  246. //点击搜索按钮
  247. $(document).on('click', '#search-btn', function () {
  248. if ($("#place").val() === '')
  249. return;
  250. var first = $(".autocomplete-search > .autocomplete-suggestion:first");
  251. if (!first.length) {
  252. return;
  253. }
  254. var item = first.data("item");
  255. //调用获取位置方法
  256. geocoder.getAddress(item.location);
  257. var position = new qq.maps.LatLng(item.location.lat, item.location.lng);
  258. map.setCenter(position);
  259. });
  260. init();
  261. });
  262. </script>
  263. </body>
  264. </html>