小程序定位并获取城市编码

描述

小程序中需要获取当前城市的信息列表

解决方法

1.定位获得经纬度

2.用百度或高德的逆地址解析获得城市编码

小程序定位并获取城市编码_第1张图片

 

百度逆地理解析文档

http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad

 

3.后台根据城市编码查询数据

 

代码

getLocation(success) {
        var that = this
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                const latitude = res.latitude
                const longitude = res.longitude
                wx.request({
                    url: request.BaiDuMapApi + 'location=' + latitude + ',' + longitude,
                    method: 'GET',
                    success: function (res) {
                        var result = res.data.result
                        var location = {
                            coordinate: result.location,
                            address: result.addressComponent
                        }
                        wx.setStorageSync('location', location)
                        var cityCode = parseInt(location.address.adcode / 100) * 100;
                        that.setData({
                            cityId: cityCode,
                        });
                       success();

                    },
                });
            }
        })
    },


 /**
     * 同城列表
     */
    getSameCityList() {
        var that = this;
        if (that.data.isAllCity) {
            that.setData({
                allCityArticleList: [],
                sameCityArticleList: [],
                isAllCity: false,
            });
            that.restScree();
            that.getLocation(
                function () {
                    that.getArticleList(1, false);
                }

            );

        }

    },


    /**
     * 帖子列表
     * @param page
     * @param isAllCity
     * @param typeId
     * @param cityId
     */
    getArticleList(page = 1, isAllCity = true) {
        var that = this
        that.setData({
            next: true
        })
        var typeIds = that.data.typeIds;
        var cityId = that.data.cityId;
        request.GET('article-list',
            {
                page: page,
                type_ids:typeIds,
                city_id: cityId,
            },
            function (res) {
                if (res.data.code == 0) {
                    var list = res.data.data.data;
                    var totalPage = res.data.data.last_page;
                    if (isAllCity) {
                        that.setData({
                            allCityArticleList: [...that.data.allCityArticleList, ...list],
                            page: page,
                            next: false
                        })
                    } else {
                        that.setData({
                            sameCityArticleList: [...that.data.sameCityArticleList, ...list],
                            page: page,
                            next: false
                        })
                    }
                    that.setData({
                        totalPage: totalPage
                    })


                } else {
                    wx.showToast({
                        title: '获取信息失败',
                        image: "/assets/icon/icon-warning.png",
                    });
                    return
                }
            },
            function (error) {
            })

    },
cityCode = parseInt(location.address.adcode / 100) * 100;   这行是为了只获取到市级代码,

附录

全国省市区三级数据库

https://download.csdn.net/download/flysnownet/12517226

 

 

 

你可能感兴趣的:(小程序)