jquery.mapjob.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. +function() {
  2. // 根据关键字搜索位置
  3. function getAcMaps(map) {
  4. var ac = new BMap.Autocomplete( //建立一个自动完成的对象
  5. {"input" : "mapSearchInput"
  6. ,"location" : map
  7. });
  8. var myValue;
  9. ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
  10. var _value = e.item.value;
  11. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  12. setPlace();
  13. });
  14. // 搜索
  15. $('#key-search-button').off().on('click', function() {
  16. var searchKey = $.trim($('#mapSearchInput').val());
  17. if (!searchKey.length) return false;
  18. myValue = searchKey;
  19. setPlace();
  20. });
  21. function setPlace(){
  22. function myFun(){
  23. var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  24. var map = new BMap.Map("mapShow");
  25. map.enableScrollWheelZoom();
  26. map.addControl(new BMap.NavigationControl());
  27. var point = new BMap.Point(pp.lng,pp.lat);
  28. map.centerAndZoom(point, 15);
  29. setMarkers(map, pp, myValue);// 创建标注
  30. setRelatePosition(map, myValue);// 设置关联信息
  31. savePoint(pp.lng, pp.lat);
  32. addClickListener(map);
  33. }
  34. var local = new BMap.LocalSearch(map, { //智能搜索
  35. onSearchComplete: myFun
  36. });
  37. local.search(myValue);
  38. setMapBounds(map);// 设置可视范围
  39. }
  40. }
  41. // 初始化地图
  42. function getMaps(lng, lat) {
  43. var map = new BMap.Map("mapShow");
  44. map.enableScrollWheelZoom();
  45. map.addControl(new BMap.NavigationControl());
  46. var point = new BMap.Point(lng,lat);
  47. map.centerAndZoom(point, 15);
  48. var myGeo = new BMap.Geocoder();
  49. var position;
  50. function geocodeSearch(pt){
  51. myGeo.getLocation(pt, function(rs){
  52. var addComp = rs.addressComponents;
  53. // 街道、区、市逐层向上找
  54. if (addComp.street.length) {
  55. position = addComp.street;
  56. } else if (addComp.district.length) {
  57. position = addComp.district;
  58. } else {
  59. position = addComp.city;
  60. }
  61. setMarkers(map, point, position);// 创建标注
  62. setRelatePosition(map, position);// 设置关联信息
  63. getAcMaps(map);// 搜索
  64. });
  65. }
  66. geocodeSearch(point); // 根据经纬度获取位置信息
  67. addClickListener(map);
  68. setMapBounds(map);// 设置可视范围
  69. }
  70. // 创建标注
  71. function setMarkers(map, point, position) {
  72. map.clearOverlays();
  73. var markerls = new BMap.Marker(point);
  74. //新建标注
  75. var infoWindow = new BMap.InfoWindow("<span style=\"font-size:14px;\">当前位置:" + position + "<br><span style=\"font-size:12px; line-height:24px;\">(提示:任意点击地图,选择您的位置)</span></span>");
  76. map.openInfoWindow(infoWindow, point);
  77. //默认时,显示窗口信息
  78. markerls.addEventListener("click", function() {
  79. map.openInfoWindow(infoWindow, point);
  80. });
  81. //点击标注点时显示窗口信息
  82. markerls.enableDragging(true);
  83. //启用地图鼠标拖拽
  84. map.addOverlay(markerls);
  85. //添加标注点在地图上
  86. }
  87. // 设置左侧相关地区
  88. function setRelatePosition(map, position) {
  89. var options = {
  90. onSearchComplete: function(results){
  91. // 判断状态是否正确
  92. if (local.getStatus() == BMAP_STATUS_SUCCESS){
  93. var sHtml = '';
  94. for (var i = 0; i < results.getCurrentNumPois(); i ++){
  95. var resultsAdr = '';
  96. if (i <= 12) {
  97. var ppArr = results.getPoi(i).point;
  98. if (results.getPoi(i).province) {
  99. resultsAdr += results.getPoi(i).province;
  100. } else if (results.getPoi(i).city) {
  101. resultsAdr += results.getPoi(i).city;
  102. } else {
  103. resultsAdr += results.getPoi(i).title;
  104. }
  105. sHtml += '<li data-position="'+ppArr.lng+','+ppArr.lat+','+results.getPoi(i).title+'"><div class="tit">'+results.getPoi(i).title+'</div><div class="adr">'+resultsAdr+'</div></li><div class="clear"></div>';
  106. }
  107. }
  108. document.getElementById("mapSearchResult").innerHTML = sHtml;
  109. }
  110. }
  111. };
  112. var local = new BMap.LocalSearch(map, options);
  113. local.search(position);
  114. }
  115. // 保存当前选中点的经纬度
  116. function savePoint(lng, lat) {
  117. $('#lng').val(lng);
  118. $('#lat').val(lat);
  119. }
  120. // 添加地图点击监听
  121. function addClickListener(map) {
  122. //创建地理编码
  123. var geoc = new BMap.Geocoder();
  124. map.addEventListener('click', function(e) {
  125. var pt = e.point;
  126. //获取新的经纬度
  127. geoc.getLocation(pt, function(rs) {
  128. //根据坐标得到地址描述
  129. var addComp = rs.addressComponents;
  130. var address = addComp.district + addComp.street + addComp.streetNumber;
  131. //获取地址
  132. setMarkers(map, pt, address);
  133. savePoint(pt.lng,pt.lat);
  134. setRelatePosition(map, address);
  135. setMapBounds(map);// 设置可视范围
  136. });
  137. });
  138. }
  139. // 设置可视范围
  140. function setMapBounds(map) {
  141. var bs = map.getBounds(); //获取可视区域
  142. var bssw = bs.getSouthWest(); //可视区域左下角
  143. var bsne = bs.getNorthEast(); //可视区域右上角
  144. $('#ldLng').val(bssw.lng);
  145. $('#ldLat').val(bssw.lat);
  146. $('#ruLng').val(bsne.lng);
  147. $('#ruLat').val(bsne.lat);
  148. //console.log("当前地图可视范围是:" + bssw.lng + "," + bssw.lat + "到" + bsne.lng + "," + bsne.lat);
  149. }
  150. // 处理跳转链接
  151. function reQsMapUrl(url) {
  152. url = url.replace('lngVal',$('#lng').val());
  153. url = url.replace('latVal',$('#lat').val());
  154. url = url.replace('ldLngVal',$('#ldLng').val());
  155. url = url.replace('ldLatVal',$('#ldLat').val());
  156. url = url.replace('ruLngVal',$('#ruLng').val());
  157. url = url.replace('ruLatVal',$('#ruLat').val());
  158. setTimeout(function() {
  159. window.location = url;
  160. }, 50)
  161. }
  162. // 显示地图搜索框
  163. function showMapDialog() {
  164. var mDialog = $(this).dialog({
  165. title: '选择位置',
  166. header: false,
  167. footer: false,
  168. border: false,
  169. content: ['<div class="map-show">', '<div class="ms-box">', '<div class="done-group pie_about">', '<div class="btn-group">', '<div id="sure-map" class="btn_yellow c-btn gre-btn">确定</div>', '<div id="cancel-map" class="btn_lightgray c-btn gry-btn">取消</div>', '<div class="clear"></div>', '</div>', '</div>', '<div>', '<div class="search-panel">', '<div class="sea-box">', '<div class="sea-container">', '<div class="sea-content">', '<input id="mapSearchInput" class="sole-input" type="text" name="word" autocomplete="off" maxlength="256" placeholder="搜地点" value="">', '</div>', '</div>', '<button id="key-search-button" class="search-button"></button>', '<div class="clear"></div>', '</div>', '</div>', '</div>', '<div class="mb-left">', '<div class="mb-title">请选择附近地标或直接搜索位置</div>', '<div class="mb-li" id="mapSearchResult">', '</div>', '</div>', '<div class="mb-right" id="mapShow">', '</div>', '<div class="clear"></div>', '</div>', '</div>'].join(''),
  170. loadFun: function() {
  171. // 设置默认地区
  172. var mapLng = '', mapLat = '';
  173. if ($('#lng').val()) {
  174. mapLng = $('#lng').val();
  175. mapLat = $('#lat').val();
  176. } else {
  177. mapLng = $('.map-lng').val();
  178. mapLat = $('.map-lat').val();
  179. }
  180. savePoint(mapLng, mapLat);
  181. getMaps(mapLng, mapLat);
  182. $("#mapSearchResult").on('click','li',function(){
  183. // 左侧定位
  184. var positionArr = $(this).data('position').split(',');
  185. var map = new BMap.Map("mapShow");
  186. map.enableScrollWheelZoom();
  187. map.addControl(new BMap.NavigationControl());
  188. var point = new BMap.Point(positionArr[0],positionArr[1]);
  189. map.centerAndZoom(point, 15);
  190. setMarkers(map, point, positionArr[2]);// 创建标注
  191. savePoint(positionArr[0], positionArr[1]);
  192. addClickListener(map);
  193. setMapBounds(map);// 设置可视范围
  194. });
  195. // 确定
  196. $('#sure-map').off().on('click', function() {
  197. $('.modal_dialog').remove();
  198. $('.modal_backdrop').remove();
  199. var ldDialog = $(this).dialog({
  200. loading: true,
  201. footer: false,
  202. header: false,
  203. border: false
  204. });
  205. reQsMapUrl(qsMapUrl);
  206. });
  207. // 取消
  208. $('#cancel-map').off().on('click', function () {
  209. savePoint(mapLng, mapLat);
  210. $('.modal_dialog').remove();
  211. $('.modal_backdrop').remove();
  212. });
  213. }
  214. });
  215. }
  216. $('#popupBox').click(function() {
  217. showMapDialog();
  218. });
  219. }(jQuery)