locate.service.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * 定位相关的业务服务
  3. */
  4. // import { qqMapKey } from '@/utils/config.js'
  5. // const QQMapWX = require('@/static/base/js/qqmap-wx-jssdk.min.js')
  6. // const qqmapsdk = new QQMapWX({
  7. // coord_type: 5,
  8. // key: qqMapKey
  9. // })
  10. export default class LocateService {
  11. constructor(AjaxService) {
  12. Object.assign(this, { AjaxService })
  13. this.name = 'LocateService'
  14. }
  15. /* 获取当前的地理位置 */
  16. getGeoLocation() {
  17. return new Promise(function(reslove, reject) {
  18. wx.getLocation({
  19. type: 'gcj02',
  20. success: function(res) {
  21. reslove(res)
  22. },
  23. fail: function(err) {
  24. reject(err)
  25. },
  26. complete: function(res) {}
  27. })
  28. })
  29. }
  30. /* 获取城市名 */
  31. getCityName(lat, lon) {
  32. return new Promise(function(reslove, reject) {
  33. qqmapsdk.reverseGeocoder({
  34. location: {
  35. latitude: lat,
  36. longitude: lon
  37. },
  38. success: function(res) {
  39. const {
  40. ad_info: addressInfo,
  41. location,
  42. formatted_addresses: formattedAddress = {},
  43. address
  44. } = res.result || {}
  45. let locationInfo = {
  46. cityName: addressInfo.city,
  47. location: location,
  48. address: formattedAddress.recommend || address
  49. }
  50. console.log("locationInfo: " + locationInfo);
  51. reslove(locationInfo)
  52. },
  53. fail: function(err) {
  54. reject(err)
  55. }
  56. })
  57. })
  58. }
  59. /* 获取当前定位 */
  60. async getLocationByLocate () {
  61. // 获取地理位置
  62. let locationInfo = await this.getGeoLocation()
  63. const { latitude, longitude } = locationInfo || {}
  64. // 获取城市名
  65. let cityInfo = await this.getCityName(latitude, longitude)
  66. const { location = {}, cityName, address } = cityInfo
  67. console.log(location);
  68. console.log(cityName);
  69. console.log(address);
  70. }
  71. }