微信小程序获取当前位置(定位)-腾讯地图

// pages/card_info/card_info.js
var config = require("../../config.js");
var util = require("../../utils/utils.js");
var city = require('../../utils/city.js');
var app = getApp();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
//[官网下载sdk](https://lbs.qq.com/qqmap_wx_jssdk/index.html)
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: app.globalData.map_key // 必填 
});


Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.getCurrentLocal()
  },

  // 获取当前地理位置 授权验证
  getCurrentLocal() {
    let that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation'] == false) { // 如果已拒绝授权,则打开设置页面
          wx.openSetting({
            success(res) {
              console.log(res)
            }
          })
        } else { // 第一次授权,或者已授权,直接调用相关api
          wx.getLocation({
            type: 'wgs84',
            success(res) {
              var param = {
                location: res.latitude + ',' + res.longitude,
                key: app.globalData.map_key,
                get_poi: 1
              }
              //下面是get去请求数据
              var url = config.qqMapApi
              // https://apis.map.qq.com/ws/geocoder/v1/
              util.postrequest(url, param, function (res) {
                var d = res.data.result
                console.log(d)
                console.log(d.address_component.city)
                that.setData({
                  citySelected: d.address_component.city,
                })
              })
            }
          })
        }
      }
    })
  },
//get方法
//get方法
function getrequest(url, data, cb) {
  wx.request({
  // https://apis.map.qq.com/ws/geocoder/v1/
    url: url,
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    data: data,
    success: function (res) {
      return typeof 'function' && cb(res)
    },
    fail: function (res) {
      wx.showToast({
        title: '网络请求失败,稍后再试',
        icon: 'none',
        duration: 2000,
      })
    },
    complete: function (res) {

    }
  })
}

//调起地图选点
//调起腾讯地图选点
getMapad: function() {
 var t = this;
 wx.getSetting({
		success(res) {
			if (res.authSetting['scope.userLocation'] == false) { // 如果已拒绝授权,则打开设置页面
				wx.openSetting({
					success(res) {
						console.log(res)
					}
				})
			} else {
				wx.chooseLocation({
					success: function(e) {
						console.log(e)
						t.setData({
							// now_location_name: e.address,
							// now_location_lat: e.latitude,
							// now_location_lng: e.longitude,
							// now_detail_address: e.name
						});
					},
					fail: function(t) {console.log(t)},
					complete: function(t) {console.log(t)}
				});
			}
		}
	})
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值