123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //index.js
- //获取应用实例
- const app = getApp();
- var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
- var qqmapsdk;
- Page({
- data: {
- motto: 'Hello World',
- userInfo: {},
- hasUserInfo: false,
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
- province: '',
- city: '',
- latitude: '',
- longitude: '',
- address: 'Hello World'
- },
- //事件处理函数
- bindViewTap: function() {
- wx.navigateTo({
- url: '../logs/logs'
- })
- },
- onLoad: function() {
- if (app.globalData.userInfo) {
- this.setData({
- userInfo: app.globalData.userInfo,
- hasUserInfo: true
- })
- } else if (this.data.canIUse) {
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- app.userInfoReadyCallback = res => {
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- } else {
- // 在没有 open-type=getUserInfo 版本的兼容处理
- wx.getUserInfo({
- success: res => {
- app.globalData.userInfo = res.userInfo
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- })
- };
- qqmapsdk = new QQMapWX({
- //这里自己的key秘钥进行填充
- key: '7KWBZ-WCFKI-2PPGL-5JPAP-EEOG6-52BOV'
- });
- },
- onShow: function() {
- let vm = this;
- vm.getUserLocation();
- },
- getUserLocation: function() {
- let vm = this;
- wx.getSetting({
- success: (res) => {
- console.log(JSON.stringify(res))
- // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
- // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
- // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
- //非初始化且用户地理位置未授权的情况
- if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
- wx.showModal({
- title: '请求授权当前位置',
- content: '需要获取您的地理位置,请确认授权',
- success: function(res) {
- if (res.cancel) {
- wx.showToast({
- title: '拒绝授权',
- icon: 'none',
- duration: 1000
- })
- } else if (res.confirm) {
- wx.openSetting({
- success: function(dataAu) {
- if (dataAu.authSetting["scope.userLocation"] == true) {
- wx.showToast({
- title: '授权成功',
- icon: 'success',
- duration: 1000
- })
- //再次授权,调用wx.getLocation的API
- vm.getLocation();
- } else {
- wx.showToast({
- title: '授权失败',
- icon: 'none',
- duration: 1000
- })
- }
- }
- })
- }
- }
- })
- } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入该页面
- //调用wx.getLocation的API
- vm.getLocation();
- } else {
- //调用wx.getLocation的API
- vm.getLocation();
- }
- }
- })
- },
- // 微信获得经纬度
- getLocation: function() {
- let vm = this;
- wx.getLocation({
- type: 'wgs84',
- success: function(res) {
- console.log(JSON.stringify(res))
- var latitude = res.latitude
- var longitude = res.longitude
- var speed = res.speed
- var accuracy = res.accuracy;
- vm.getLocal(latitude, longitude)
- },
- fail: function(res) {
- console.log('fail' + JSON.stringify(res))
- }
- })
- },
- // 获取当前地理位置
- getLocal: function(latitude, longitude) {
- let vm = this;
- qqmapsdk.reverseGeocoder({
- location: {
- latitude: latitude,
- longitude: longitude
- },
- success: function(res) {
- console.log(res);
- console.log(JSON.stringify(res));
- let province = res.result.ad_info.province
- let city = res.result.ad_info.city
- let address = res.result.address
- vm.setData({
- province: province,
- city: city,
- latitude: latitude,
- longitude: longitude,
- address: address
- })
- },
- fail: function(res) {
- console.log(res);
- },
- complete: function(res) {
- // console.log(res);
- }
- });
- },
- getUserInfo: function(e) {
- console.log(e)
- app.globalData.userInfo = e.detail.userInfo
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- }
- })
|