map.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>Baidu Maps</title>
  6. <style>
  7. html {
  8. height: 100%;
  9. }
  10. body {
  11. height: 100%;
  12. margin: 0;
  13. padding: 0;
  14. background-color: #fff;
  15. }
  16. #search_box {
  17. position: fixed;
  18. top: 5px;
  19. right: 5px;
  20. z-index: 100;
  21. }
  22. #search_box input {
  23. -webkit-appearance: none;
  24. border-radius: 3px;
  25. box-sizing: border-box;
  26. outline: 0;
  27. box-shadow: 0 0 3px rgba(0, 0, 0, 0.4);
  28. }
  29. #search_box input[type="text"] {
  30. background-color: #cccccc;
  31. border: 1px solid #ccc;
  32. color: #207ab7;
  33. width: 180px;
  34. padding: 5px;
  35. font-weight: bold;
  36. font-size: 16px;
  37. opacity: 0.7;
  38. box-shadow: 0 0 3px rgba(0, 0, 0, 0.4);
  39. }
  40. #search_box input[type="button"] {
  41. margin-left: 5px;
  42. background-color: #207ab7;
  43. border: 1px solid #207ab7;
  44. color: #fff;
  45. padding: 4px 6px;
  46. font-size: 14px;
  47. }
  48. #div-show-list {
  49. position: fixed;
  50. top:37px;
  51. right:57px;
  52. z-index: 200;
  53. background:white;
  54. font-weight: 400;
  55. width:178px;font-size: 14px;
  56. color: #7274A7;border:1px solid #ccc;
  57. }
  58. #div-show-list .div-item:hover {
  59. color: #009E94;
  60. background-color: #ebcccc;
  61. font-weight: 700;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div id="search_box">
  67. <input id="kw_input" class="kw_input" type="text" value="" autocomplete="off" placeholder="输入要搜索的地点"/>
  68. <input type="button" value="搜索" onclick="searchMapByStationName()"></div>
  69. <div id="div-show-list" style="display:block;"></div>
  70. <div id="map_canvas" style="width:100%; height:100%"></div>
  71. </body>
  72. <script type="text/javascript" src="jquery.js"></script>
  73. <script charset="utf-8" src="https://api.map.baidu.com/api?v=2.0&ak=QXSZyPZk26shrYzAXjTkDLx5LbRCHECz"></script>
  74. <script>
  75. var map;
  76. initialize();
  77. /**
  78. * 页面初始化加载
  79. */
  80. function initialize() {
  81. map = new BMap.Map('map_canvas');
  82. var point = new BMap.Point(116.331398, 39.897445);
  83. map.centerAndZoom(point, 19);
  84. map.addControl(new BMap.NavigationControl());
  85. ////开启鼠标滚轮缩放
  86. map.enableScrollWheelZoom(true);
  87. //根据IP定位
  88. var myCity = new BMap.LocalCity();
  89. myCity.get(function (result) {
  90. map.setCenter(result.name);
  91. });
  92. /**
  93. * 百度地图 监听点击事件
  94. */
  95. map.addEventListener("click", function (e) {
  96. var ponitE = e.point;
  97. var marker = new BMap.Marker(new BMap.Point(ponitE.lng, ponitE.lat));
  98. map.clearOverlays();
  99. map.addOverlay(marker);
  100. map.panTo(ponitE);
  101. parent.tinymceLng = ponitE.lng;
  102. parent.tinymceLat = ponitE.lat;
  103. console.log('经纬度:',ponitE); // 经纬度数据
  104. }
  105. );
  106. }
  107. document.getElementById('kw_input').addEventListener('keypress', function (e) {
  108. //回车键效果
  109. if (e.keyCode == '13') {
  110. e.preventDefault();
  111. searchMapByStationName();
  112. }
  113. });
  114. /**
  115. * 关键字 搜索输入框触发事件,保证实时的数据刷新显示效果
  116. */
  117. $(".kw_input").bind("input propertychange", function () {
  118. var val = $(".kw_input").val();
  119. var options = {
  120. onSearchComplete: function (results) {
  121. if (localSearch.getStatus() == BMAP_STATUS_SUCCESS) {
  122. // 判断状态是否正确
  123. if (results.getCurrentNumPois() == 0) {
  124. console.log('搜索不到该地区');
  125. $("#div-show-list").css("display", "none");
  126. } else {
  127. var searchResHtml = "";
  128. for (var i = 0; i < results.getCurrentNumPois(); i++) {
  129. searchResHtml += "<div style='padding:5px;cursor:pointer' class='div-item'>" + results.getPoi(i).title + "</div>";
  130. }
  131. $("#div-show-list").html(searchResHtml);
  132. $("#div-show-list").css("display", "block");
  133. }
  134. }
  135. }
  136. };
  137. var localSearch = new BMap.LocalSearch(map, options);
  138. localSearch.search(val);
  139. });
  140. /**
  141. * js动态加载 html元素,html中绑定的click事件不生效的解决办法
  142. * @param obj
  143. */
  144. $("body").on('click', '.div-item', function () {
  145. //do sth...
  146. $("#kw_input").val($(this).html());
  147. $("#div-show-list").css("display", "none");
  148. searchMapByStationName();
  149. });
  150. /**
  151. * 根据输入的文字 搜索对应的百度地图信息
  152. * @returns {boolean}
  153. */
  154. function searchMapByStationName() {
  155. var keyword = document.getElementById("kw_input").value;
  156. var local = new BMap.LocalSearch(map, {renderOptions: {map: map}});
  157. local.setSearchCompleteCallback(function (searchResult) {
  158. console.log(searchResult);
  159. var poi = searchResult.getPoi(0);
  160. if(poi){
  161. var pointNew = poi.point;
  162. map.centerAndZoom(pointNew, 19);
  163. var marker = new BMap.Marker(new BMap.Point(pointNew.lng, pointNew.lat));
  164. parent.tinymceLng = pointNew.lng;
  165. parent.tinymceLat = pointNew.lat;
  166. map.clearOverlays();
  167. map.addOverlay(marker);
  168. map.panTo(pointNew);
  169. }else {
  170. console.log('Sorry,未查到地址信息')
  171. }
  172. });
  173. local.search(keyword);
  174. }
  175. </script>
  176. </html>