----------------------------wx.getLocation wx.chooseLocation--------------------------------------
获取经纬度,打开地图选择位置
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html
https://developers.weixin.qq.com/community/develop/doc/000a02f2c5026891650e7f40351c01
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html
----------------------------wxmapsdk.reverseGeocoder--------------------------------------
逆地址解析(坐标位置描述) 获取南北经纬度可转换为详细的地址
var WXMapSdk = require('../../mapsdk/qqmap-wx-jssdk1_2/qqmap-wx-jssdk');
var wxmapsdk = new WXMapSdk({
key: 'PTIBZ-NDGNF-GTNJO-JEWRJ-T3TVT-VPFPO'
https://blog.csdn.net/qq_42754787/article/details/108833670
https://lbs.qq.com/faq/accountQuota/faqKey
https://lbs.qq.com/dev/console/application/mine
https://mp.weixin.qq.com/wxamp/devprofile/get_profile?token=1210844988&lang=zh_CN
http://3gimg.qq.com/lightmap/xcx/jssdk/qqmap-wx-jssdk1.2.zip
https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodReverseGeocoder
-
错误:请求来源未被授权, 解决方法见:https://lbs.qq.com/faq/serverFaq/webServiceKey。此次请求来源域名:servicewechat.com
解决:将servicewechat.com添加到https://lbs.qq.com/dev/console/application/mine的项目key配置中
-
错误:getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化
解决:从基础库2.17.0版本开始,将会对getLocation 接口进行频率限制,包括在开发版本和体验版本中,30s内调用除第一次后,剩余返回fail。但是在正式版本中将不会走fail块,剩余会返回与第一次定位相同的信息。此处带来的限制是在30s内只会触发一次来限制频率,所以不需要太担心,并不会影响业务逻辑
解决:清缓存,重新编译运行
解决:wxmapsdk.reverseGeocoder()未指定location参数,将wx.getLocation获取到的longitude和latitude传入即可
代码示例:
var WXMapSdk = require('../../mapsdk/qqmap-wx-jssdk1_2/qqmap-wx-jssdk');
var wxmapsdk = new WXMapSdk({
key: 'ABC-DEF-XXX-XYZ'
});
---
Page({
data: {
province: '', //省
city: '', //市
district: '', //行政区
street: '', //小区
street_number: '', //街道号
address: '', //地址
latitude: 0, //北纬度
longitude: 0, //东经度
},
},
---
// 事件处理函数
getUserProfile(e) {
let that = this
// 获取用户位置
wx.getLocation({
type: 'gcj02',
success (res) {
console.log(res)
var longitude = res.longitude
var latitude = res.latitude
that.setData({
'personLocation.longitude': longitude,
'personLocation.latitude': latitude,
});
//存入app全局变量中
var app = getApp()
app.globalData.personLocation.longitude = longitude
app.globalData.personLocation.latitude = latitude
// 逆地址解析 经纬度位置转化为字符串位置
let handler = that
wxmapsdk.reverseGeocoder({
location: {
longitude: longitude,
latitude: latitude
},
success: function(res) {
console.log(res)
var province = res.result.address_component.province
var city = res.result.address_component.city
var district = res.result.address_component.district
var street = res.result.address_component.street
var street_number = res.result.address_component.street_number
var address = res.result.address + res.result.formatted_addresses.recommend
handler.setData({
'personLocation.province': province,
});
//存入app全局变量中
var app = getApp()
app.globalData.personLocation.province = province
app.globalData.personLocation.city = city
app.globalData.personLocation.district = district
app.globalData.personLocation.street = street
app.globalData.personLocation.street_number = street_number
app.globalData.personLocation.address = address
console.log(app.globalData.personLocation.province)
console.log(app.globalData.personLocation.city)
console.log(app.globalData.personLocation.district)
console.log(app.globalData.personLocation.street)
console.log(app.globalData.personLocation.street_number)
console.log(app.globalData.personLocation.address)
},
fail: function(res) {
console.log(res);
}
})
}
})
返回数据:
错误:getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化
解决:从基础库2.17.0版本开始,将会对getLocation 接口进行频率限制,包括在开发版本和体验版本中,30s内调用除第一次后,剩余返回fail。但是在正式版本中将不会走fail块,剩余会返回与第一次定位相同的信息。此处带来的限制是在30s内只会触发一次来限制频率,所以不需要太担心,并不会影响业务逻辑
解决:清缓存,重新编译运行
解决:wxmapsdk.reverseGeocoder()未指定location参数,将wx.getLocation获取到的longitude和latitude传入即可
错误:请求来源未被授权, 解决方法见:https://lbs.qq.com/faq/serverFaq/webServiceKey。此次请求来源域名:servicewechat.com
解决:将servicewechat.com添加到https://lbs.qq.com/dev/console/application/mine的项目key配置中