123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /**
- * 定位相关的业务服务
- */
- // import { qqMapKey } from '@/utils/config.js'
- // const QQMapWX = require('@/static/base/js/qqmap-wx-jssdk.min.js')
- // const qqmapsdk = new QQMapWX({
- // coord_type: 5,
- // key: qqMapKey
- // })
- export default class LocateService {
- constructor(AjaxService) {
- Object.assign(this, { AjaxService })
- this.name = 'LocateService'
- }
- /* 获取当前的地理位置 */
- getGeoLocation() {
- return new Promise(function(reslove, reject) {
- wx.getLocation({
- type: 'gcj02',
- success: function(res) {
- reslove(res)
- },
- fail: function(err) {
- reject(err)
- },
- complete: function(res) {}
- })
- })
- }
- /* 获取城市名 */
- getCityName(lat, lon) {
- return new Promise(function(reslove, reject) {
- qqmapsdk.reverseGeocoder({
- location: {
- latitude: lat,
- longitude: lon
- },
- success: function(res) {
- const {
- ad_info: addressInfo,
- location,
- formatted_addresses: formattedAddress = {},
- address
- } = res.result || {}
- let locationInfo = {
- cityName: addressInfo.city,
- location: location,
- address: formattedAddress.recommend || address
- }
- console.log("locationInfo: " + locationInfo);
- reslove(locationInfo)
- },
- fail: function(err) {
- reject(err)
- }
- })
- })
- }
- /* 获取当前定位 */
- async getLocationByLocate () {
- // 获取地理位置
- let locationInfo = await this.getGeoLocation()
- const { latitude, longitude } = locationInfo || {}
- // 获取城市名
- let cityInfo = await this.getCityName(latitude, longitude)
- const { location = {}, cityName, address } = cityInfo
- console.log(location);
- console.log(cityName);
- console.log(address);
- }
- }
|