微信小程序实现自动定位功能

使用了腾讯地图提供的免费api

需要引入一个js文件:下载地址

js代码:

// 引入SDK核心类 var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); var qqmap = new QQMapWX({ //在腾讯地图开放平台申请密钥 key: 'your KEY' }); Page({ data: { myLatitude: "", myLongitude: "", myAddress: "" }, onLoad: function(){ var that = this //用微信提供的api获取经纬度 wx.getLocation({ type: 'wgs84', success: function(res){ that.setData({myLatitude: res.latitude, myLongitude: res.longitude}) //用腾讯地图的api,根据经纬度获取城市 qqmap.reverseGeocoder({ location: { latitude: that.data.myLatitude, longitude: that.data.myLongitude }, success: function (res) { console.log(res) var a = res.result.address_component //获取市和区(区可能为空) that.setData({myAddress: a.city + a.district}) //控制台输出结果 console.log(that.data.myAddress) } }) } }) } })

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c418e0a53660781fd25255936cd44764.html