> 文档中心 > uniapp 小程序使用腾讯地图搜索位置地点,获取省、市、县地区码的方法

uniapp 小程序使用腾讯地图搜索位置地点,获取省、市、县地区码的方法

一、调用uni.authorize()方法获取用户授权

二、调用官方uni.chooseLocation()方法获取当前地址的名称和经纬度

uni.chooseLocation({success: (res) => {    console.log(res.latitude)    console.log(res.longitude)}});

但是此方法只能获取到经纬度,并不能直接获取到省、市、县的地区码,因此我们需要进行 “逆地理位置解析”reverseGeocoder(options:Object)方法来获取,详情见:WebService API | 腾讯位置服务

 三、登录腾讯位置服务注册账号,申请key值,引入封装好的腾讯地图JS模块

js模块引入地址:https://mapapi.qq.com/web/mapComponents/geoLocation/v/geolocation.min.js

import map from "../../common/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js" // 实例化API核心类var qqmapsdk = new map({key: 'key值' // 必填,去腾讯地图申请});qqmapsdk.reverseGeocoder({//Object格式location: {latitude: “当前位置纬度”,longitude: “当前位置经度”},success: (res) => {console.log(res.result.ad_info); //位置信息},fail: (error) => {// console.error(error);}});

整体详情代码如下:

import map from "../../common/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js";export default {    methods: { maphq() {uni.authorize({scope: 'scope.userLocation',success: () => {uni.chooseLocation({success: (res) => {  // 实例化API核心类var mapsdk = new map({key: 'key值' // 必填});mapsdk.reverseGeocoder({//Object格式location: {latitude: res.latitude,longitude: res.longitude},success: (res) => {console.log(res.result.ad_info); //城市信息},fail: (error) => {// console.error(error);}});}});},fail: () => {uni.showModal({title: '提示',content: '位置权限获取失败,请重新授权!',success: function(res) {if (res.confirm) {console.log('用户点击确定');this.ditu()} else if (res.cancel) {console.log('用户点击取消');}}});}})},    }}

 欢迎指正