shop-map.js 12 KB

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