首页 > 编程语言 >#yyds干货盘点#【愚公系列】2022年12月 微信小程序-项目篇(公交查询)-02周边站点-获取位置和城市信息

#yyds干货盘点#【愚公系列】2022年12月 微信小程序-项目篇(公交查询)-02周边站点-获取位置和城市信息

时间:2022-12-02 20:34:09浏览次数:60  
标签:yyds 12 res poi param location 微信 data options

前言

1.相关API

逆地址解析:提供由经纬度到文字地址及相关位置信息的转换能力,广泛应用于物流、出行、O2O、社交等场景。服务响应速度快、稳定,支撑亿级调用。

可以满足以下相关业务场景:

  • 满足传统对省市区、乡镇村、门牌号、道路及交叉口、河流、湖泊、桥、poi列表的需求。
  • 提供通过知名地点、地标组合形成的易于理解的地址,如:北京市海淀区中钢国际广场(欧美汇购物中心北)。
  • 提供精准的商圈、知名的大型区域、附近知名的一级地标、代表当前位置的二级地标等。

请求地址:https://apis.map.qq.com/ws/geocoder/v1/?location=

请求参数:

参数 必填 说明 示例
location 经纬度(GCJ02坐标系),格式:location=lat<纬度>,lng<经度> location= 39.984154,116.307490
get_poi 是否返回周边地点(POI)列表,可选值:0 不返回(默认),1 返回 get_poi=1
poi_options 周边POI列表控制参数:1 poi_options=address_format=short返回短地址,缺省时返回长地址2 poi_options=radius=5000半径,取值范围1-5000(米)3 poi_options=policy=1/2/3/4/5控制返回场景,policy=1[默认] 以地标+主要的路+近距离POI为主,着力描述当前位置;policy=2 到家场景:筛选合适收货的POI,并会细化收货地址,精确到楼栋;policy=3 出行场景:过滤掉车辆不易到达的POI(如一些景区内POI),增加道路出入口、交叉口、大区域出入口类POI,排序会根据真实API大用户的用户点击自动优化。policy=4 社交签到场景,针对用户签到的热门 地点进行优先排序。policy=5 位置共享场景,用户经常用于发送位置、位置分享等场景的热门地点优先排序4 注:policy=1/2/3最多返回10条周边POI,policy=4/5最多返回20条,如需更多请参见地点搜索-周边推荐 【单个参数写法示例】:poi_options=address_format=short【多个参数英文分号间隔,写法示例】:poi_options=address_format=short;radius=5000;policy=2
key 开发密钥(Key) key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
output 返回格式:支持JSON/JSONP,默认JSON output=json
callback JSONP方式回调函数 callback=function1

详情请看相关接口网址:https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder

一、获取位置和城市信息

获取位置方法:getLocationInfo

getLocationInfo() {
    var _this = this
    app.showLoading("拉取路线列表")
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var locationInfo = _this.data.locationInfo
        locationInfo.latitude = res.latitude
        locationInfo.longitude = res.longitude
        // 调用接口
        qqmap.reverseGeocoder({
          location: {
            latitude: locationInfo.latitude,
            longitude: locationInfo.longitude
          },
          success: function (res) {
            locationInfo.city = res.result.address_component.city
            locationInfo.address = res.result.formatted_addresses.recommend
            _this.setData({
              locationInfo: locationInfo
            })
            _this.getStationList()
          },
          fail: function (res) {
            console.log(res);
            app.hideLoading(locationInfo)
          },
          complete: function (res) {
            // complete
            // console.log(_this.data.locationInfo)
          }
        })
      }
    })
  },

逆地址解析方法:reverseGeocoder

reverseGeocoder(options) {
   var that = this;
   options = options || {};
   Utils.polyfillParam(options);
   var requestParam = {
       coord_type: options.coord_type || 5,
       get_poi: options.get_poi || 0,
       output: 'json',
       key: that.key
   };
   if (options.poi_options) {
       requestParam.poi_options = options.poi_options
   }

   var locationsuccess = function (result) {
       requestParam.location = result.latitude + ',' + result.longitude;
       wx.request(Utils.buildWxRequestConfig(options, {
           url: URL_GET_GEOCODER,
           data: requestParam
       }));
   };
   Utils.locationProcess(options, locationsuccess);
}

Utils相关工具类:

/**
 * 回调函数默认处理
 */
polyfillParam(param) {
    param.success = param.success || function () { };
    param.fail = param.fail || function () { };
    param.complete = param.complete || function () { };
},

/**
 * 构造微信请求参数,公共属性处理
 * 
 * @param {Object} param 接口参数
 * @param {Object} param 配置项
 */
buildWxRequestConfig(param, options) {
    var that = this;
    options.header = { "content-type": "application/json" };
    options.method = 'GET';
    options.success = function (res) {
        var data = res.data;
        if (data.status === 0) {
            param.success(data);
        } else {
            param.fail(data);
        }
    };
    options.fail = function (res) {
        res.statusCode = ERROR_CONF.WX_ERR_CODE;
        param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, result.errMsg));
    };
    options.complete = function (res) {
        var statusCode = +res.statusCode;
        switch(statusCode) {
            case ERROR_CONF.WX_ERR_CODE: {
                param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
                break;
            }
            case ERROR_CONF.WX_OK_CODE: {
                var data = res.data;
                if (data.status === 0) {
                    param.complete(data);
                } else {
                    param.complete(that.buildErrorConfig(data.status, data.message));
                }
                break;
            }
            default:{
                param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
            }

        }
    }
    return options;
},

/**
 * 处理用户参数是否传入坐标进行不同的处理
 */
locationProcess(param, locationsuccess, locationfail, locationcomplete) {
    var that = this;
    locationfail = locationfail || function (res) {
        res.statusCode = ERROR_CONF.WX_ERR_CODE;
        param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
    };
    locationcomplete = locationcomplete || function (res) {
        if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
            param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
        }
    };
    if (!param.location) {
        that.getWXLocation(locationsuccess, locationfail, locationcomplete);
    } else if (that.checkLocation(param)) {
        var location = Utils.getLocationParam(param.location);
        locationsuccess(location);
    }
}
/**
 * 构造错误数据结构
 * @param {Number} errCode 错误码
 * @param {Number} errMsg 错误描述
 */
buildErrorConfig(errCode, errMsg) {
    return {
        status: errCode,
        message: errMsg
    };
},
/**
 * 获取location参数
 */
getLocationParam(location) {
    if (typeof location == 'string') {
        var locationArr = location.split(',');
        if (locationArr.length === 2) {
            location = {
                latitude: location.split(',')[0],
                longitude: location.split(',')[1]
            };
        } else {
            location = {};
        }
    }
    return location;
},

标签:yyds,12,res,poi,param,location,微信,data,options
From: https://blog.51cto.com/u_15437432/5907430

相关文章