微信小程序-自动定位并将经纬度解析为具体地址

微信小程序可以通过API获取当前位置的经纬度。

在微信小程序开发文档中可以找到这个API的使用示例。

https://developers.weixin.qq.com/miniprogram/dev/api/location.html


但是需要获取具体地址 如:湖南省长沙市岳麓区****,就需要使用到外部的API(此处用到的是腾讯的位置服务)

这是开发文档 里面有具体步骤和示例 http://lbs.qq.com/qqmap_wx_jssdk/index.html

自动获取地理位置可用于签到,具体实现如下。

一、index.wxml 显示经纬度及地址数据

<!--index.wxml-->


< view wx:if= "{{latitude}}" mode= "widthFix" >纬度:{{latitude}} </ view >
< view wx:if= "{{longitude}}" mode= "widthFix" >经度:{{longitude}} </ view >
< view wx:if= "{{address}}" mode= "widthFix" >地址:{{address}} </ view >

二、index.js 调用API获取数据

//index.js
var QQMapWX = require ( '../../util/qqmap-wx-jssdk.js' ) //引入获得地址的js文件
var qqmapsdk ;
const app = getApp ()
Page ({
data : {
latitude : null ,
longitude : null ,
address : null ,
},
onLoad : function () {
this .getLocation (); //一进来就得到地址
} ,
getLocation () {
var that = this
wx .getLocation ({//调用API得到经纬度
type : 'wgs84' ,
success : function (res ) {
var speed = res .speed
var accuracy = res .accuracy
var latitude = res .latitude
var longitude = res .longitude

that .setData ({
latitude : res .latitude ,
longitude : res .longitude
})
//地址解析
var demo = new QQMapWX ({
key : '****' // 这个KEY的获取方式在上面链接 腾讯位置服务的开发文档中有详细的申请密钥步骤
});

demo .reverseGeocoder ({//地址解析
location : {
latitude : latitude ,
longitude : longitude
},
success : function (res ) {
console .log (res );
//获得地址
that .setData ({
address : res .result .address
})
},
fail : function (res ) {
console .log (res );
},
complete : function (res ) {
console .log (res );
}
});
}

})
}
})


  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值