当前位置:   article > 正文

uni-app获取位置_uniapp 获取定位

uniapp 获取定位
一、获取当前的地理位置、速度:uni.getLocation(OBJECT)
1、申请腾讯地图的key:在https://lbs.qq.com/中注册,登录后,单击网站右上角的“控制台”,进入控制台页面

2、在manifest.json文件中配置腾讯地图
  1. "h5": {
  2. "sdkConfigs": {
  3. "maps": {
  4. "qqmap": {
  5. "key": "你的key"
  6. }
  7. }
  8. }
  9. }
3、配合map组件定位"我的位置"

(1)map的属性

longitude(类型为Number,没有默认值,表示中心经度)

latitude(类型为Number,没有默认值,表示中心纬度)

markers(类型为Array数组,类型为数组即表示地图上可以有多个,没有默认值,表示标记点)

  1. <template>
  2. <view>
  3. <map class="map" :latitude="latitude" :longitude="longitude" :markers="markers"></map>
  4. </view>
  5. </template>
  6. <script>
  7. export default{
  8. data(){
  9. return{
  10. latitude:"", //纬度
  11. longitude: "",//经度
  12. markers:[ //标记点用于在地图上显示标记的位置
  13. {
  14. id: 1,
  15. latitude: "", //纬度
  16. longitude: "" ,//经度
  17. iconPath: "/static/images/business_map/my.png", //图标路径
  18. width: "30", //图标的宽
  19. height: "30" //图标的高
  20. }
  21. ]
  22. }
  23. },
  24. onLoad() {
  25. uni.getLocation({
  26. type: 'gcj02', //国家测绘局坐标
  27. success: (res) => {
  28. console.log("当前位置的经度:",res.longitude);
  29. console.log("当前位置的纬度:",res.latitude);
  30. this.latitude = res.latitude
  31. this.longitude = res.longitude
  32. this.markers[0].latitude = this.latitude
  33. this.markers[0].longitude = this.longitude
  34. }
  35. })
  36. }
  37. }
  38. </script>
  39. <style>
  40. .map{
  41. width: 100%;
  42. height: 500rpx;
  43. }
  44. </style>
二、微信小程序获取详细地址
1、下载微信小程序JavaScript SDK
2、将sdk放入项目的static下
  1. <template>
  2. <view>
  3. <map class="map" :latitude="latitude" :longitude="longitude" :markers="markers"></map>
  4. <view>城市:{{ city }}</view>
  5. <view>省份:{{ province }}</view>
  6. <view>区(县):{{ district }}</view>
  7. <view>街道:{{ street }}</view>
  8. <view>门牌号:{{ streetNum }}</view>
  9. <view>城市代码:{{ cityCode }}</view>
  10. <view>地址详情:{{ address }}</view>
  11. </view>
  12. </template>
  13. <script>
  14. import QQMapWX from '../../static/js/qqmap-wx-jssdk.js'
  15. export default{
  16. data(){
  17. return{
  18. latitude:"", //纬度
  19. longitude: "",//经度
  20. markers:[ //标记点用于在地图上显示标记的位置
  21. {
  22. id:1,
  23. latitude: "", //纬度
  24. longitude: "" ,//经度
  25. iconPath: "/static/images/business_map/my.png", //图标路径
  26. width: "30", //图标的宽
  27. height: "30", //图标的高
  28. callout:{ //自定义标记点上方的气泡窗口
  29. content: "", //文本内容
  30. display: "ALWAYS", //常显示气泡
  31. padding: 5, //文本边缘留白
  32. borderRadius: 8, //callout边框圆角
  33. fontSize: 14 //文字大小
  34. }
  35. }
  36. ],
  37. city: "",
  38. province: "",
  39. district: "",
  40. street: "",
  41. streetNum: "",
  42. cityCode: "",
  43. address: ""
  44. }
  45. },
  46. onLoad: () => {
  47. qqmapsdk = new QQMapWX({
  48. key: "X5YBZ-G4HLG-X3AQI-QHDGT-G73G3-62BF4"
  49. })
  50. uni.getLocation({
  51. type: 'gcj02', //国家测绘局坐标
  52. success: (res) => {
  53. console.log("当前位置的经度:",res.longitude);
  54. console.log("当前位置的纬度:",res.latitude);
  55. this.latitude = res.latitude
  56. this.longitude = res.longitude
  57. this.markers[0].latitude = this.latitude
  58. this.markers[0].longitude = this.longitude
  59. //获取地理位置信息和附近的POI列表
  60. qqmapsdk.reverseGeocoder({
  61. location: {
  62. latitude: this.latitude,
  63. longitude: this.longitude
  64. },
  65. //返回成功
  66. success: (res)=>{
  67. console.log(res);
  68. this.city = res.result.ad_info.city
  69. this.province = res.result.ad_info.province
  70. this.district = res.result.ad_info.district
  71. this.street = res.result.address_component.street
  72. this.streetNum = res.result.address_component.street_number
  73. this.cityCode = res.result.ad_info.city_code
  74. this.address = res.result.address
  75. //将获取的城市赋值到气泡content中,在map组件中显示
  76. this.markers[0].callout.content = this.city
  77. },
  78. fail:(err)=>{
  79. console.log(err);
  80. }
  81. })
  82. }
  83. })
  84. }
  85. }
  86. </script>
  87. <style>
  88. .map{
  89. width: 100%;
  90. height: 500rpx;
  91. }
  92. </style>

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

闽ICP备14008679号