shop-map.js 12 KB

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