tencent.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. </style>
  46. </head>
  47. <body>
  48. <div class="search">
  49. <div class="input-group">
  50. <input type="text" id="place" name="q" class="form-control" placeholder="输入地点"/>
  51. <span class="input-group-btn">
  52. <button type="submit" name="search" id="search-btn" class="btn btn-success">
  53. <i class="fa fa-search"></i>
  54. </button>
  55. </span>
  56. </div>
  57. </div>
  58. <div class="confirm">确定</div>
  59. <div id="container"></div>
  60. <script charset="utf-8" src="//map.qq.com/api/js?v=2.exp&libraries=place&key={$config.tencentkey|default=''}"></script>
  61. <script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
  62. <script type="text/javascript">
  63. $(function () {
  64. var map, marker, geocoder, infoWin, searchService, address = null;
  65. var init = function () {
  66. var center = new qq.maps.LatLng({$lat}, {$lng});
  67. map = new qq.maps.Map(document.getElementById('container'), {
  68. center: center,
  69. zoom: parseInt("{$config.zoom}")
  70. });
  71. //初始化marker
  72. initmarker(center);
  73. //实例化信息窗口
  74. infoWin = new qq.maps.InfoWindow({
  75. map: map
  76. });
  77. geocoder = new qq.maps.Geocoder({
  78. complete: function (result) {
  79. infoWin.open();
  80. address = result.detail.addressComponents.province +
  81. result.detail.addressComponents.city +
  82. result.detail.addressComponents.district;
  83. if (result.detail.addressComponents.streetNumber == '') {
  84. address += result.detail.addressComponents.street;
  85. } else {
  86. address += result.detail.addressComponents.streetNumber;
  87. }
  88. infoWin.setContent(address);
  89. infoWin.setPosition(result.detail.location);
  90. }
  91. });
  92. //显示当前marker的位置信息窗口
  93. geocoder.getAddress(center);
  94. var latlngBounds = new qq.maps.LatLngBounds();
  95. //查询poi类信息
  96. searchService = new qq.maps.SearchService({
  97. complete: function (results) {
  98. console.log(results);
  99. if (typeof results.detail.pois === 'undefined') {
  100. alert("未搜索到相关信息");
  101. return;
  102. }
  103. var pois = results.detail.pois;
  104. for (var i = 0, l = pois.length; i < l; i++) {
  105. var poi = pois[i];
  106. latlngBounds.extend(poi.latLng);
  107. initmarker(poi.latLng);
  108. //显示当前marker的位置信息窗口
  109. geocoder.getAddress(poi.latLng);
  110. }
  111. map.fitBounds(latlngBounds);
  112. }
  113. });
  114. //实例化自动完成
  115. var ap = new qq.maps.place.Autocomplete(document.getElementById('place'));
  116. //添加监听事件
  117. qq.maps.event.addListener(ap, "confirm", function (res) {
  118. searchKeyword.call(this, res.value);
  119. });
  120. qq.maps.event.addListener(
  121. map,
  122. 'click',
  123. function (event) {
  124. try {
  125. infoWin.setContent('<div style="text-align:center;white-space:nowrap;margin:10px;">加载中</div>');
  126. var latLng = event.latLng,
  127. lat = latLng.getLat().toFixed(5),
  128. lng = latLng.getLng().toFixed(5);
  129. var location = new qq.maps.LatLng(lat, lng);
  130. //调用获取位置方法
  131. geocoder.getAddress(location);
  132. infoWin.setPosition(location);
  133. marker.setPosition(location);
  134. } catch (e) {
  135. console.log(e);
  136. }
  137. }
  138. );
  139. };
  140. //实例化marker和监听拖拽结束事件
  141. var initmarker = function (latLng) {
  142. marker = new qq.maps.Marker({
  143. map: map,
  144. position: latLng,
  145. draggable: true,
  146. title: '拖动图标选择位置'
  147. });
  148. //监听拖拽结束
  149. qq.maps.event.addListener(marker, 'dragend', function (event) {
  150. var latLng = event.latLng,
  151. lat = latLng.getLat().toFixed(5),
  152. lng = latLng.getLng().toFixed(5);
  153. var location = new qq.maps.LatLng(lat, lng);
  154. //调用获取位置方法
  155. geocoder.getAddress(location);
  156. });
  157. };
  158. var close = function (data) {
  159. var index = parent.Layer.getFrameIndex(window.name);
  160. var callback = parent.$("#layui-layer" + index).data("callback");
  161. //再执行关闭
  162. parent.Layer.close(index);
  163. //再调用回传函数
  164. if (typeof callback === 'function') {
  165. callback.call(undefined, data);
  166. }
  167. };
  168. //执行搜索方法
  169. var searchKeyword = function () {
  170. searchService.clear();//先清除
  171. marker.setMap(null);
  172. infoWin.close();
  173. var keyword = $("#place").val();
  174. // searchService.setLocation("北京");//设置默认检索范围(默认为全国),类型可以是坐标或指定的城市名称。
  175. searchService.setPageIndex(0);//设置检索的特定页数。
  176. searchService.setPageCapacity(1);//设置每页返回的结果数量。
  177. searchService.search(this.place.address + " " + keyword);//开始查询
  178. };
  179. //点击确定后执行回调赋值
  180. $(document).on('click', '.confirm', function () {
  181. var as = marker.getPosition();
  182. var x = as.getLat().toFixed(5);
  183. var y = as.getLng().toFixed(5);
  184. var zoom = map.getZoom();
  185. var data = {lat: x, lng: y, zoom: zoom, address: address};
  186. close(data);
  187. });
  188. //点击搜索按钮
  189. $(document).on('click', '#search-btn', function () {
  190. if ($("#place").val() == '')
  191. return;
  192. searchKeyword();
  193. });
  194. init();
  195. });
  196. </script>
  197. </body>
  198. </html>