小程序之当前地址获取

在小程序中获取地理位置信息的流程就是:

1.根据wx.getLocation方法获取当前位置坐标。

2.根据reverseGeocoder方法获取当前坐标地理位置信息。 

具体实现如下:

//1.1引入sdk核心类(写在app.js中,其他页面用到直接引用)
var
QQMapWX = require('/utils/qqmap-wx-jssdk.min.js'); //app.js App({ onLaunch: function () { var that = this;
     //1.2实例化腾讯地图API核心类 that.globalData.qqmapsdk
= new QQMapWX({ key: '开发秘钥' });      //1.3wx.getLocation方法获取当前位置坐标。 wx.getLocation({ altitude:false, success: function (res) { var latitude = res.latitude; var longitude = res.longitude; that.globalData.location={ latitude: latitude, longitude: longitude } } }); }, globalData: { url: '' } })

说明:根据wx.getLocation方法获取当前位置坐标。这个是写在app.js中,某个页面用的时候在某页面的js文件中根据qqmapsdk.reverseGeocoder方法获取当前坐标地理位置信息。 

onLoad: function (options) {

        var that = this;


        app.globalData.qqmapsdk.reverseGeocoder({       //qqmapsdk.reverseGeocoder

            location: {
                latitude: app.globalData.location.latitude,
                longitude: app.globalData.location.longitude
            },
            success: function (res) {

                var address = res.result.address;

                that.setData({

                    current_address: address

                });

            },
            fail: function (res) {

                wx.showToast({
                    title: '解析地址错误',
                    icon: 'loading',
                    duration: 1000
                });

            },

        })


    }

 

 
posted @ 2018-05-15 11:49  wanan_01  阅读(6206)  评论(3编辑  收藏  举报