point.htm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />
  6. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  7. <meta name="apple-mobile-web-app-capable" content="yes">
  8. <title>地图定位</title>
  9. <link rel="stylesheet" type="text/css" href="__STATIC__/member/default/wap/global.css">
  10. <link rel="stylesheet" type="text/css" href="__STATIC__/member/default/wap/wap.css">
  11. <script type="text/javascript" src="__STATIC__/js/core/jquery.min.js"></script>
  12. <script type="text/javascript" src="__STATIC__/layer/layer.js"></script>
  13. <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=MGdbmO6pP5Eg1hiPhpYB0IVd"></script>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
  17. <script type="text/javascript" src="__STATIC__/js/map-gps.js"></script>
  18. <style type="text/css">
  19. .bmap{width:100%;height:350px;border: 1px solid #ccc;}
  20. .searchResultPanel{border:1px solid #C0C0C0;width:150px;height:auto;display:none;}
  21. .baaapsou{width: 10%;float: left;cursor:pointer;height: 28px;line-height: 28px;text-align: center;border: 1px solid #C0C0C0;border-radius: 3px;margin-left: 1%;}
  22. body{max-width:100%;}
  23. .WapFormContainer .butter{
  24. margin-top:40px;
  25. }
  26. .WapFormContainer .butter button{
  27. width:100%;
  28. }
  29. </style>
  30. <div class="WapFormContainer" >
  31. <div class="js-bmap">
  32. <div class="searchResultPanel"></div>
  33. <input class="bmap-point" type="hidden" id="atc_map" name="map" value="{$map}">
  34. <div class="bmap" id="bmap-canvas-map"></div>
  35. <input class="bmap-address" style="width:85%;float: left;" id="bmap-address-map" name="map_address" type="text" value="" placeholder="可输入要搜索的地址,或手工定位">
  36. <div id="baaapsou-map" class="baaapsou">搜索</div>
  37. </div>
  38. <div class='butter'><button type="submit" onclick="post()">确认</button></div>
  39. </div>
  40. <script type="text/javascript">
  41. function post(){
  42. var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
  43. parent.layer.close(index);
  44. parent.end_choose_map( $("#atc_map").val() );
  45. }
  46. //公众号或WAP浏览器获取当前坐标位置
  47. function reload_baidu_map(obj){
  48. var geolocation = new BMap.Geolocation();
  49. geolocation.getCurrentPosition(function(result){
  50. if(this.getStatus() == window.BMAP_STATUS_SUCCESS){
  51. map_x = result.point.lng;
  52. map_y = result.point.lat;
  53. obj.val(map_x+','+map_y);
  54. if(typeof(set_baidu_map)=='function'){set_baidu_map();}
  55. //显示当前位置的街道名
  56. var geoc = new BMap.Geocoder();
  57. geoc.getLocation(result.point, function(rs){
  58. var addComp = rs.addressComponents;
  59. $("#atc_address").val(addComp.district + addComp.street + addComp.streetNumber);
  60. });
  61. //gg = GPS.bd_decrypt(result.point.lat, result.point.lng); //百度转谷歌
  62. //wgs = GPS.gcj_decrypt(gg.lat, gg.lon); //谷歌转GPS
  63. //showMapPosition(wgs.lon,wgs.lat);
  64. } else {
  65. alert('failed:'+this.getStatus());
  66. }
  67. },{enableHighAccuracy: true})
  68. }
  69. //小程序获取当前坐标位置
  70. function load_wx_map(obj){
  71. wx.getLocation({
  72. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入''
  73. success: function (res) {
  74. var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
  75. var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
  76. var o = GPS.bd_encrypt(res.latitude,res.longitude);
  77. map_x = o.lon;//res.longitude;
  78. map_y = o.lat;//res.latitude;
  79. obj.val(map_x+','+map_y);
  80. if(typeof(set_baidu_map)=='function'){set_baidu_map();}
  81. $.get("/qun/near/point_address.html?x=" + map_y + "&y=" + map_x ,function(res){
  82. $("#atc_address").val(res.data);
  83. });
  84. },
  85. cancel: function (res) {
  86. alert('用户拒绝授权获取地理位置');
  87. }
  88. });
  89. }
  90. var is_in_wxapp = -1;
  91. function get_location_point(obj){
  92. wx.miniProgram.getEnv(function(res) {
  93. if(res.miniprogram==true){ //在小程序中,用不了百度地图定位
  94. is_in_wxapp = 1;
  95. wx.config({
  96. debug: false,
  97. appId: '',
  98. timestamp: 0,
  99. nonceStr: '',
  100. signature: '',
  101. jsApiList: [
  102. 'openLocation',
  103. 'getLocation'
  104. ]
  105. });
  106. wx.ready(function () {
  107. load_wx_map(obj);
  108. });
  109. wx.error(function (res) {
  110. alert(res.errMsg);
  111. });
  112. }else{
  113. is_in_wxapp = 0;
  114. reload_baidu_map(obj); //在公众号或WAP浏览器里
  115. }
  116. });
  117. setTimeout(function(){
  118. if(is_in_wxapp==-1){
  119. reload_baidu_map(obj);
  120. }
  121. },1500);
  122. }
  123. </script>
  124. <script type="text/javascript">
  125. jQuery(document).ready(function() {
  126. set_baidu_map();
  127. });
  128. function set_baidu_map(){
  129. $('.js-bmap').each(function() {
  130. var that = $(this);
  131. var map_canvas = that.find('.bmap').attr('id');
  132. var address = that.find('.bmap-address');
  133. var address_id = address.attr('id');
  134. var map_point = that.find('.bmap-point');
  135. var search_result = that.find('.searchResultPanel');
  136. var point_lng = 113.268332;
  137. var point_lat = 23.130274;
  138. var map_level = that.data('level');
  139. // 百度地图API功能
  140. var map = new BMap.Map(map_canvas);
  141. //开启鼠标滚轮缩放
  142. map.enableScrollWheelZoom(true);
  143. // 左上角,添加比例尺
  144. var top_left_control = new BMap.ScaleControl({
  145. anchor: BMAP_ANCHOR_TOP_LEFT
  146. });
  147. // 左上角,添加默认缩放平移控件
  148. var top_left_navigation = new BMap.NavigationControl();
  149. map.addControl(top_left_control);
  150. map.addControl(top_left_navigation);
  151. // 智能搜索
  152. var local = new BMap.LocalSearch(map, {
  153. onSearchComplete: function() {
  154. var point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  155. map.centerAndZoom(point, 18);
  156. // 创建标注
  157. create_mark(point);
  158. }
  159. });
  160. // 发起检索
  161. $("#baaapsou-map").click(function() {
  162. var city = document.getElementById("bmap-address-map").value;
  163. if (city != "") {
  164. local.search(city);
  165. }
  166. });
  167. // 创建标注
  168. var create_mark = function(point) {
  169. // 清空所有标注
  170. map.clearOverlays();
  171. var marker = new BMap.Marker(point); // 创建标注
  172. map.addOverlay(marker); //添加标注
  173. marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
  174. // 写入坐标
  175. map_point.val(point.lng + "," + point.lat);
  176. };
  177. // 建立一个自动完成的对象
  178. var ac = new BMap.Autocomplete({
  179. "input": address_id,
  180. "location": map
  181. });
  182. // 鼠标放在下拉列表上的事件
  183. ac.addEventListener("onhighlight", function(e) {
  184. var str = "";
  185. var _value = e.fromitem.value;
  186. var value = "";
  187. if (e.fromitem.index > -1) {
  188. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  189. }
  190. str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
  191. value = "";
  192. if (e.toitem.index > -1) {
  193. _value = e.toitem.value;
  194. value = _value.province + _value.city + _value.district + _value.street + _value.business;
  195. }
  196. str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
  197. search_result.html(str);
  198. });
  199. // 鼠标点击下拉列表后的事件
  200. var myValue;
  201. ac.addEventListener("onconfirm", function(e) {
  202. var _value = e.item.value;
  203. myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
  204. search_result.html("onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue);
  205. local.search(myValue);
  206. });
  207. // 监听点击地图时间
  208. map.addEventListener("click", function(e) {
  209. // 创建标注
  210. create_mark(e.point);
  211. });
  212. //解决部分手机的兼容问题
  213. map.addEventListener("touchend", function (e) {
  214. map.disableDragging();
  215. create_mark(e.point);
  216. });
  217. map.addEventListener("touchmove", function (e) {
  218. map.enableDragging();
  219. });
  220. if (map_point.val() != '') {
  221. var curr_point = map_point.val().split(',');
  222. point_lng = curr_point[0];
  223. point_lat = curr_point[1];
  224. map_level = 16;
  225. } else if (address.val() != '') {
  226. local.search(address.val());
  227. } else {
  228. // 根据ip获取当前城市,并定位到当前城市
  229. var myCity = new BMap.LocalCity();
  230. myCity.get(function(result) {
  231. var cityName = result.name;
  232. map.setCenter(cityName);
  233. });
  234. }
  235. // 初始化地图,设置中心点坐标和地图级别
  236. var point = new BMap.Point(point_lng, point_lat);
  237. map.centerAndZoom(point, map_level);
  238. if (map_point.val() != '') {
  239. // 创建标注
  240. create_mark(point);
  241. }
  242. if (address.val() != '') {
  243. ac.setInputValue(address.val())
  244. }
  245. if(map_point.val() == '113.263661,23.155131'||map_point.val() == ''){ //新发表的时候,获取当前定位坐标
  246. get_location_point(map_point);
  247. }
  248. });
  249. }
  250. </script>
  251. </body>
  252. </html>