当前位置:   article > 正文

uniapp 获取当前位置和地理信息(微信小程序和APP)_uniapp微信小程序定位

uniapp微信小程序定位

一、在微信小程序中获取位置信息

1、首先引入amap-wx.130.js文件,在项目中可以新建文件夹common,在下面引入该js文件

2、在使用的页面引入该js,

  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import amap from '@/common/amap-wx.130.js'
  6. export default {
  7. data() {
  8. return {
  9. amapPlugin:null
  10. }
  11. }
  12. }

3、获取定位信息(注意先需要申请高德地图的key)申请方式如下连接

vue+高德地图/ el-amap-CSDN博客

  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import amap from '@/common/amap-wx.130.js'
  6. export default {
  7. data() {
  8. return {
  9. amapPlugin:null
  10. }
  11. },
  12. async onLoad(item) {
  13. this.amapPlugin = new amap.AMapWX({
  14. key: '你的key值'
  15. });
  16. var that = this
  17. this.amapPlugin.getRegeo({
  18. success: (data) => {
  19. console.log('小程序获取定位成功')
  20. console.log(222, data)
  21. // 经度
  22. that.data3.lng = data[0].longitude;
  23. // 纬度
  24. that.data3.lat = data[0].latitude;
  25. // 省
  26. that.data3.province = data[0].regeocodeData.addressComponent.province
  27. // 市
  28. that.data3.city = data[0].regeocodeData.addressComponent.city
  29. // 区
  30. that.data3.district = data[0].regeocodeData.addressComponent.district
  31. uni.setStorageSync('longitude', that.data3.lng)
  32. uni.setStorageSync('latitude', that.data3.lat)
  33. uni.setStorageSync('longitude2', that.data3.lng)
  34. uni.setStorageSync('latitude2', that.data3.lat)
  35. uni.setStorageSync('province', that.data3.province)
  36. uni.setStorageSync('city', that.data3.city)
  37. uni.setStorageSync('district', that.data3.district)
  38. },
  39. fail: function(res) {
  40. console.log(res)
  41. }
  42. });
  43. }
  44. }

二、app获取位置信息

  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import amap from '@/common/amap-wx.130.js'
  6. export default {
  7. data() {
  8. return {
  9. amapPlugin:null
  10. }
  11. },
  12. methods:{
  13. async chooseSpotApp() {
  14. let that = this
  15. uni.getLocation({
  16. type: 'gcj02',
  17. isHighAccuracy: true,
  18. geocode: true,
  19. success: function(res) {
  20. console.log(res)
  21. // 经度
  22. that.data3.lng = res.longitude;
  23. // 纬度
  24. that.data3.lat = res.latitude;
  25. // 省
  26. that.data3.province = res.address.province
  27. // 市
  28. that.data3.city = res.address.city
  29. // 区
  30. that.data3.district = res.address.district
  31. uni.setStorageSync('longitude', that.data3.lng)
  32. uni.setStorageSync('latitude', that.data3.lat)
  33. uni.setStorageSync('longitude2', that.data3.lng)
  34. uni.setStorageSync('latitude2', that.data3.lat)
  35. uni.setStorageSync('province', that.data3.province)
  36. uni.setStorageSync('city', that.data3.city)
  37. uni.setStorageSync('district', that.data3.district)
  38. let key = '替换你的key'; //高德地图key
  39. uni.request({
  40. url: 'https://restapi.amap.com/v3/geocode/regeo?location=' + that
  41. .longitude + ',' + that.latitude + '&key=' + key,
  42. success: (res) => {
  43. console.log('高德地图API接口返回信息', res.data.regeocode.addressComponent)
  44. uni.setStorageSync('sheng', res.data.regeocode.addressComponent
  45. .province)
  46. },
  47. fail: (error) => {
  48. console.log(error)
  49. }
  50. })
  51. },
  52. fail(res) {
  53. console.log(111, res)
  54. }
  55. });
  56. },
  57. }
  58. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/922239
推荐阅读
相关标签
  

闽ICP备14008679号