navigation.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view>
  3. <map id="tencentMap" :style="[...mapStyle]" :show-compass="showCompass" :enable-traffic="enableTraffic"
  4. :longitude="longitude" :latitude="latitude" :markers="markers" :show-location="showLocation"
  5. @markertap="markertap"></map>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. mapStyle: {
  12. type: Object,
  13. default: {
  14. width: '100%',
  15. height: '600rpx'
  16. }
  17. },
  18. showCompass: { // 是否显示指南针
  19. type: Boolean,
  20. default: true
  21. },
  22. enableTraffic: { // 是否开启实时路况
  23. type: Boolean,
  24. default: false
  25. },
  26. showLocation: { // 是否显示带有方向的当前定位点
  27. type: Boolean,
  28. default: true
  29. },
  30. },
  31. data() {
  32. return {
  33. longitude: '118.72495',
  34. latitude: '32.00834',
  35. markers: [],
  36. }
  37. },
  38. onReady() {
  39. let vm = this
  40. vm.map = uni.createMapContext('tencentMap', this)
  41. vm.getLocation()
  42. },
  43. onReady() {
  44. // wx请求获取位置权限
  45. this.getAuthorize().then(() => {
  46. // 同意后获取
  47. this.getLocationInfo();
  48. }).catch(() => {
  49. // 不同意给出弹框,再次确认
  50. this.openConfirm().then(() => {
  51. this.getLocationInfo();
  52. }).catch(() => {
  53. this.rejectGetLocation();
  54. });
  55. });
  56. },
  57. methods: {
  58. // 初次位置授权
  59. getAuthorize() {
  60. return new Promise((resolve, reject) => {
  61. uni.authorize({
  62. scope: "scope.userLocation",
  63. success: () => {
  64. resolve(); // 允许授权
  65. },
  66. fail: () => {
  67. reject(); // 拒绝授权
  68. },
  69. });
  70. });
  71. },
  72. // 确认授权后,获取用户位置
  73. getLocationInfo() {
  74. const that = this;
  75. uni.getLocation({
  76. type: "gcj02",
  77. success: function(res) {
  78. that.longitude = res.longitude;
  79. that.latitude = res.latitude;
  80. that.markers = [{
  81. id: 2,
  82. latitude: 32.00834,
  83. longitude: 118.72495,
  84. callout: {
  85. content: '奥林皮克体育中心',
  86. color: '#000',
  87. fontSize: 10,
  88. bgColor: '#fff',
  89. padding: 5,
  90. display: 'ALWAYS',
  91. textAlign: 'center'
  92. }
  93. }, ]
  94. }
  95. });
  96. },
  97. // 拒绝授权后,弹框提示是否手动打开位置授权
  98. openConfirm() {
  99. return new Promise((resolve, reject) => {
  100. uni.showModal({
  101. title: "请求授权当前位置",
  102. content: "我们需要获取地理位置信息,为您推荐附近的商家",
  103. success: (res) => {
  104. if (res.confirm) {
  105. uni.openSetting().then((res) => {
  106. if (res[1].authSetting["scope.userLocation"] === true) {
  107. resolve(); // 打开地图权限设置
  108. } else {
  109. reject();
  110. }
  111. });
  112. } else if (res.cancel) {
  113. reject();
  114. }
  115. },
  116. });
  117. });
  118. },
  119. // 彻底拒绝位置获取
  120. rejectGetLocation() {
  121. uni.showToast({
  122. title: "您拒绝了授权,无法获得周边信息",
  123. icon: "none",
  124. duration: 2000,
  125. });
  126. },
  127. // 地图标点 点击事件
  128. markertap(e) {
  129. let plugin = requirePlugin('routePlan');
  130. let key = '5ZKBZ-W3TCU-LSDVC-4XJLB-KQOYO-TVBQ6'; // 使用在腾讯位置服务申请的key
  131. let referer = 'test'; // 调用插件的app的名称
  132. let navigation = 1 // 值为1时,开启驾车导航功能;默认不开启此功能
  133. let endPoint = JSON.stringify({ // 终点
  134. 'name': '奥林皮克体育中心',
  135. 'latitude': 32.00834,
  136. 'longitude': 118.72495
  137. });
  138. wx.navigateTo({
  139. url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint +
  140. '&navigation=' + navigation
  141. });
  142. }
  143. },
  144. }
  145. </script>
  146. <style>
  147. </style>