shop-map.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var app = new Vue({
  2. 'el': '#app',
  3. 'data': {
  4. curNav: 'map',
  5. // 列表数据
  6. dataList: [],
  7. // 参数
  8. params: {
  9. page: 1,
  10. pageSize: 6
  11. },
  12. point: null,
  13. cpoint: null,
  14. map: null,
  15. mapParams: {
  16. level: 13,
  17. lng: 108.325449,
  18. lat: 22.823939,
  19. },
  20. info: {lng: 108.325449, lat: 22.823939},
  21. type: 0,
  22. loading: false,
  23. loaded: false,
  24. },
  25. created: function () {
  26. this.params.type = getParam('type');
  27. this.initPoint();
  28. },
  29. mounted: function () {
  30. this.initMap();
  31. },
  32. methods: {
  33. // 初始化定位
  34. initPoint: function () {
  35. var lng = sessionStorage.getItem('point_lng');
  36. var lat = sessionStorage.getItem('point_lat');
  37. if (lng) {
  38. this.mapParams.lng = lng;
  39. }
  40. if (lat) {
  41. this.mapParams.lat = lat;
  42. }
  43. },
  44. // 地图初始化
  45. initMap: function () {
  46. var _this = this;
  47. _this.map = new BMap.Map('map');
  48. _this.point = new BMap.Point(_this.mapParams.lng, _this.mapParams.lat);
  49. _this.cpoint = new BMap.Point(_this.info.lng, _this.info.lat);
  50. // 创建点坐标
  51. _this.map.centerAndZoom(_this.point, _this.mapParams.level);
  52. _this.map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
  53. _this.map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
  54. _this.map.disableDoubleClickZoom(); //启用地图惯性拖拽,默认禁用
  55. _this.loadLocation();
  56. _this.loadMark(_this.point);
  57. },
  58. // 微信地图定位
  59. loadLocation: function () {
  60. var _this = this;
  61. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  62. var params = res.data;
  63. // 微信JSSDK
  64. wx.config({
  65. debug: false, // 是否调试模式
  66. appId: params.appId, // 必填,公众号的唯一标识
  67. timestamp: params.timestamp, // 必填,生成签名的时间戳
  68. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  69. signature: params.signature,// 必填,签名
  70. jsApiList: ['getLocation'] // 必填,需要使用的JS接口列表
  71. });
  72. // 初始化处理
  73. wx.ready(function () {
  74. wx.getLocation({
  75. type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  76. success: function (res) {
  77. var convertor = new BMap.Convertor();
  78. var ggPoint = new BMap.Point(res.longitude, res.latitude);
  79. var pointArr = [];
  80. pointArr.push(ggPoint);
  81. convertor.translate(pointArr, 1, 5, function (data){
  82. if(data.status === 0) {
  83. res.longitude = data.points[0].lng;
  84. res.latitude = data.points[0].lat;
  85. sessionStorage.setItem('point_lng', res.longitude)
  86. sessionStorage.setItem('point_lat', res.latitude)
  87. var point = new BMap.Point(res.longitude, res.latitude);
  88. var gc = new BMap.Geocoder();
  89. gc.getLocation(point, function (rs) {
  90. var addComp = rs.addressComponents;
  91. address = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
  92. sessionStorage.setItem('city', r.address.city)
  93. sessionStorage.setItem('address', address)
  94. });
  95. alert(JSON.stringify(res));
  96. point = {lng: res.longitude, lat: res.latitude};
  97. var mk = new BMap.Marker(point);
  98. _this.map.addOverlay(mk);
  99. _this.map.centerAndZoom(point, _this.mapParams.level);
  100. _this.map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
  101. _this.map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
  102. //alert('当前定位:'+JSON.stringify(r.point));
  103. _this.loadMark(point);
  104. }
  105. });
  106. }
  107. });
  108. });
  109. }, "json");
  110. },
  111. // 百度地图定位
  112. baiduLoadLocation: function () {
  113. var _this = this;
  114. var geolocation = new BMap.Geolocation();
  115. $.showLoading('获取定位中...')
  116. geolocation.enableSDKLocation();
  117. geolocation.getCurrentPosition(function (r) {
  118. $.hideLoading();
  119. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  120. var mk = new BMap.Marker(r.point);
  121. var address = r.address.province + ' ' + r.address.city + ' ' + r.address.district;
  122. sessionStorage.setItem('point_lng', r.point.lng)
  123. sessionStorage.setItem('point_lat', r.point.lat)
  124. sessionStorage.setItem('city', r.address.city)
  125. sessionStorage.setItem('address', address)
  126. _this.map.addOverlay(mk);
  127. _this.map.centerAndZoom(r.point, _this.mapParams.level);
  128. _this.map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
  129. _this.map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
  130. //alert('当前定位:'+JSON.stringify(r.point));
  131. _this.loadMark(r.point);
  132. }
  133. else {
  134. $.toast('获取您的位置信息失败', 'text');
  135. }
  136. }, {enableHighAccuracy: true, maximumAge: 1000});
  137. },
  138. // 坐标转换
  139. transPoint: function(point){
  140. var convertor = new BMap.Convertor();
  141. var ggPoint = new BMap.Point(point.lng, point.lat);
  142. var pointArr = [];
  143. pointArr.push(ggPoint);
  144. convertor.translate(pointArr, 1, 5, function (data){
  145. if(data.status === 0) {
  146. point.lng = data.points[0].lng;
  147. point.lat = data.points[0].lat;
  148. }
  149. });
  150. },
  151. // 加载地图数据
  152. /**
  153. * 加载车棚数据
  154. * @param lng 纬度
  155. * @param lat 经度
  156. */
  157. loadMark: function (point) {
  158. if (point) {
  159. var _this = this;
  160. $.showLoading('获取数据中...')
  161. // $.post('/weixin/shop/getList', {lng: '108.26120200', lat: '23.17164900'}, function (res) {
  162. $.post('/weixin/shop/getList', {lng: point.lng, lat: point.lat}, function (res) {
  163. $.hideLoading();
  164. if (res.code == 'success') {
  165. var dataList = res.data;
  166. if (dataList.length > 0) {
  167. $.each(dataList, function (k, item) {
  168. _this.cpoint = new BMap.Point(item.lng, item.lat);
  169. _this.makeOverlay(_this.cpoint, item);
  170. });
  171. } else {
  172. $.toast('附近暂无商家', "text")
  173. }
  174. } else if (res.code == 'login') {
  175. login(res.data.url);
  176. } else {
  177. $.toast(res.message, "text")
  178. }
  179. }, "JSON");
  180. }
  181. },
  182. // 地图加载
  183. makeOverlay: function (cpoint, data) {
  184. function ComplexCustomOverlay(point, text) {
  185. this._point = point;
  186. this._text = text;
  187. }
  188. ComplexCustomOverlay.prototype = new BMap.Overlay();
  189. ComplexCustomOverlay.prototype.initialize = function (map) {
  190. this._map = map;
  191. var div = this._div = document.createElement("div");
  192. div.style.position = "absolute";
  193. // div.style.zIndex = 600+data.id;
  194. div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
  195. div.style.textAlign = "center";
  196. div.style.whiteSpace = "nowrap";
  197. div.style.MozUserSelect = "none";
  198. div.setAttribute('class', 'info');
  199. div.setAttribute('data-id', data.id);
  200. div.setAttribute('data-point', this._point.lng + ',' + this._point.lat);
  201. var span = this._span = document.createElement("span");
  202. div.appendChild(span);
  203. span.appendChild(document.createTextNode(this._text));
  204. // 添加点击事件
  205. div.addEventListener("touchstart", function () {
  206. var _info = $(this);
  207. _info.addClass('active').siblings('.info').removeClass('active');
  208. var mobile = data.mobile ? data.mobile : '';
  209. var intro = data.intro ? data.intro : '无';
  210. var colorType = data.color_type==1? '黑白打印机': (data.color_type==2? '彩印打印机' : '黑白/彩色打印机');
  211. var address = data.d_address ? data.d_address : (data.address? data.address : '无');
  212. var shop_name = data.shop_name ? '('+data.shop_name+')' : '';
  213. $.modal({
  214. title: '设备[' + data.device_name + ']',
  215. text: "<div class='item address'>设备号:" + data.device_code + "</div><div class='item'>打印机类型:"+colorType+"</div><div class='item mobile'>联系方式:<a href='tel:" + mobile + "'>联系店家"+shop_name+"</a></div><div class='item address'>店铺地址:" + address + "</div><div class='intro'>店铺介绍:" + intro + "</div>",
  216. buttons: [
  217. {
  218. text: "取消",
  219. className: "default",
  220. onClick: function () {
  221. $.closeModal();
  222. }
  223. },
  224. {
  225. text: "设备导航",
  226. onClick: function(){
  227. location.href = "http://api.map.baidu.com/marker?location=" + data.lat + "," + data.lng + "&title=" + data.device_name + "&content=" +address+ "&output=html";
  228. }
  229. },
  230. ]
  231. });
  232. });
  233. map.getPanes().labelPane.appendChild(div);
  234. return div;
  235. }
  236. ComplexCustomOverlay.prototype.draw = function () {
  237. var map = this._map;
  238. var pixel = map.pointToOverlayPixel(this._point);
  239. this._div.style.left = pixel.x - 40 + "px";
  240. this._div.style.top = pixel.y-100 + "px";
  241. }
  242. // 初始化覆盖物数据
  243. var text = data.device_name;
  244. var myCompOverlay = new ComplexCustomOverlay(cpoint, text, text);
  245. this.map.addOverlay(myCompOverlay);
  246. }
  247. }
  248. })