当前位置:   article > 正文

uni-app 引用地图_uniapp引用地圖

uniapp引用地圖

插件市场引用为 https://ext.dcloud.net.cn/plugin?id=615

 插件引用后进行了部分修改

目录

 

index页面 

 组件页面  【创建nvue文件】

生成高德地图Key


index页面 

  1. <template>
  2. <div>
  3. <hchPosition :storeData="storeData" :markers="markers"></hchPosition>
  4. </div>
  5. <!-- <div class="content">
  6. <map :scale='18' id="myMap" style="width: 100%;" :style="{height: mapHeight + 'px'}"
  7. :markers="markers" enable-satellite="true" :longitude="longitude" :latitude="latitude" :circles="circles"></map>
  8. </div> -->
  9. </template>
  10. <script>
  11. //引用组件 uni-app中创建nvue文件
  12. // import hchPosition from '@/components/hch-position/hch-position.vue'
  13. import hchPosition from '@/components/hch-position/hch-position.nvue'
  14. export default {
  15. data() {
  16. return {
  17. latitude: 0,
  18. longitude: 0,
  19. circles: [],//圆
  20. storeData: [ //门店信息展示 id name address tel 必填
  21. {
  22. id: 1,
  23. name: '热源',
  24. address: '热源1号站',
  25. tel: '热源1号站:瞬时热量、瞬时流量、单耗',
  26. },
  27. {
  28. id: 2,
  29. name: '热源',
  30. address: '热源2号站',
  31. tel: '热源2号站:瞬时热量、瞬时流量、单耗',
  32. }, {
  33. id: 3,
  34. name: '换热站',
  35. address: '换热站1号',
  36. tel: '换热站1:瞬时热量、瞬时流量、单耗',
  37. },
  38. ],
  39. markers: [ //门店在地图上的标记 以下字段必填
  40. {
  41. id: 1,
  42. latitude: 39.03,
  43. // latitude: 39.939203,
  44. longitude: 116.210689,
  45. iconPath: '/static/hch-position/ry1.png',
  46. callout: {
  47. content: "热源1号站",
  48. borderRadius: 10,
  49. padding: 10,
  50. display: "ALWAYS",
  51. }
  52. }, {
  53. id: 2,
  54. latitude: 39.945441,
  55. longitude: 116.217556,
  56. iconPath: '/static/hch-position/ry2.png',
  57. callout: {
  58. content: "热源2号站",
  59. borderRadius: 10,
  60. padding: 10,
  61. display: "ALWAYS",
  62. }
  63. },{
  64. id: 3,
  65. latitude: 39.8226,
  66. longitude: 116.250166,
  67. iconPath: '/static/hch-position/hrz1.png',
  68. callout: {
  69. content: "换热站1号",
  70. borderRadius: 10,
  71. padding: 10,
  72. display: "ALWAYS",
  73. }
  74. }
  75. ],
  76. }
  77. },
  78. components: {
  79. hchPosition,
  80. },
  81. created() {
  82. // this.getLocationInfo()
  83. },
  84. methods: {
  85. // 获取地理位置
  86. // getLocationInfo() {
  87. // console.log(111)
  88. // var that = this
  89. // uni.getLocation({
  90. // type: 'wgs84',
  91. // success(res) {
  92. // console.log(res, "当前位置");
  93. // that.latitude = res.latitude
  94. // that.longitude = res.longitude
  95. // console.log(res.latitude+"——"+res.longitude)
  96. // //标记点
  97. // that.markers = [{
  98. // id: 1,
  99. // latitude: res.latitude,
  100. // longitude: res.longitude,
  101. // iconPath: '/static/hch-position/wd.png',
  102. // }]
  103. // that.circles = [{ //在地图上显示圆
  104. // latitude: res.latitude,
  105. // longitude: res.longitude,
  106. // fillColor: "#D9E6EF", //填充颜色
  107. // color: "#A7B6CB", //描边的颜色
  108. // radius: 50, //半径
  109. // strokeWidth: 2 //描边的宽度
  110. // }]
  111. // }
  112. // });
  113. // },
  114. }
  115. }
  116. </script>
  117. <style>
  118. </style>

 

 组件页面  【创建nvue文件】

  1. <template>
  2. <div>
  3. <div class="page-body">
  4. <div class="page-section page-section-gap">
  5. <map id="myMap" style="width: 100%; height: 300px;" show-compass="true" enable-satellite="true"
  6. :latitude="latitude" @anchorpointtap="anchorpointtap" :longitude="longitude" :markers="markers"
  7. show-location="true" enable-scroll enable-zoom @tap="bindtap" :circles="circles"
  8. @callouttap="callouttap" @regionchange="regionchange" @updated="updated">
  9. </map>
  10. </div>
  11. <div class="store-tips">
  12. <div class="store-des-box" v-if="storeFlag" @tap="storeDesEvn">
  13. <image class="store-img" :src="storemark"></image>
  14. <!-- <image class="store-img" src="/static/img/dzd.png"></image> -->
  15. <div class="store-des">
  16. <div class="store-name">{{store.name}}</div>
  17. <div class="store-address">地址:{{store.address}}</div>
  18. <div class="store-tel">电话:{{store.tel}}</div>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="near-num">距离最近的门店{{distanceL}}千米</div>
  23. <!-- <image class="address-icon" src="/static/Home.png"></image> -->
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. export default {
  29. props: {
  30. storeData: {
  31. type: Array,
  32. },
  33. markers: {
  34. type: Array,
  35. }
  36. },
  37. data() {
  38. return {
  39. store: {},
  40. storeFlag: false,
  41. storeAdFlag: true,
  42. distanceL: 0, //附近店门的距离
  43. latitude: 39,
  44. longitude: 116,
  45. circles: [], //圆
  46. controls: [],
  47. storemark: {},
  48. }
  49. },
  50. watch: {
  51. latitude(val) {
  52. this.latitude = val
  53. },
  54. longitude(val) {
  55. this.longitude = val
  56. }
  57. },
  58. mounted() {
  59. console.log(this.latitude)
  60. console.log(this.markers)
  61. this.getLocationInfo()
  62. },
  63. methods: {
  64. getLocationInfo() {
  65. console.log(111)
  66. var that = this
  67. uni.getLocation({
  68. type: 'wgs84',
  69. success(res) {
  70. console.log(res, "当前位置");
  71. that.latitude = res.latitude
  72. that.longitude = res.longitude
  73. console.log(res.latitude + "——" + res.longitude)
  74. //标记点
  75. that.markers.push({
  76. id: 1,
  77. latitude: res.latitude,
  78. longitude: res.longitude,
  79. iconPath: '/static/hch-position/wddw.png',
  80. })
  81. that.circles = [{ //在地图上显示圆
  82. latitude: res.latitude,
  83. longitude: res.longitude,
  84. fillColor: "#D9E6EF", //填充颜色
  85. color: "#A7B6CB", //描边的颜色
  86. radius: 50, //半径
  87. strokeWidth: 2 //描边的宽度
  88. }]
  89. }
  90. });
  91. },
  92. // 点击了门店信息
  93. storeDesEvn() {
  94. uni.showToast({
  95. title: "自己自定义跳门店详情页介绍",
  96. duration: 2000,
  97. icon: "none"
  98. })
  99. },
  100. // 点击气泡
  101. callouttap(e) {
  102. this.storeFlag = true
  103. this.storeAdFlag = false
  104. for (let i = 0; i < this.storeData.length; i++) {
  105. const ele = this.storeData[i]
  106. // debugger
  107. console.log(e.detail.markerId)
  108. if (ele.id == e.detail.markerId) {
  109. this.store = ele
  110. this.storemark = this.markers[i].iconPath
  111. break
  112. }
  113. }
  114. },
  115. anchorpointtap(e) {
  116. console.log(666666666666666)
  117. console.log(e.detail)
  118. },
  119. // 点击地图
  120. bindtap(id) {
  121. this.storeFlag = false
  122. // this.storeAdFlag=true;
  123. },
  124. // 隐藏广告图片
  125. // hideAd() {
  126. // this.storeAdFlag = false
  127. // },
  128. updated() {
  129. },
  130. // 改变视野时改变经纬度
  131. regionchange(e) {
  132. let _this = this
  133. // 使用 wx.createMapContext 获取 map 上下文 this必须要写 不然没办法得到回调!!!!。
  134. // var mapCtx = wx.createMapContext("myMap", this)
  135. var mapCtx = uni.createMapContext("myMap", this)
  136. mapCtx.getCenterLocation({
  137. success: function(res) {
  138. _this.centerLatitude = res.latitude
  139. _this.centerLongitude = res.longitude
  140. _this.nearDistance(
  141. _this.markers,
  142. _this.centerLatitude,
  143. _this.centerLongitude
  144. )
  145. },
  146. fail: function(res) {
  147. console.log(res)
  148. }
  149. }) //获取当前地图的中心经纬度
  150. },
  151. // 两点间距离
  152. distance(la1, lo1, la2, lo2) {
  153. var La1 = (la1 * Math.PI) / 180.0
  154. var La2 = (la2 * Math.PI) / 180.0
  155. var La3 = La1 - La2
  156. var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
  157. var s =
  158. 2 *
  159. Math.asin(
  160. Math.sqrt(
  161. Math.pow(Math.sin(La3 / 2), 2) +
  162. Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
  163. )
  164. )
  165. s = s * 6378.137 //地球半径
  166. s = Math.round(s * 10000) / 10000
  167. return s
  168. },
  169. // 计算最近的距离
  170. nearDistance(array, centerLatitude, centerLongitude) {
  171. let temp = []
  172. for (let i = 0, l = array.length; i < l; i++) {
  173. const element = array[i]
  174. let d = this.distance(
  175. element.latitude,
  176. element.longitude,
  177. centerLatitude,
  178. centerLongitude
  179. )
  180. temp.push(d)
  181. }
  182. this.distanceL = Math.min.apply(null, temp)
  183. }
  184. },
  185. onShow() {},
  186. onReady() {}
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .content {
  191. text-align: center;
  192. height: 400rpx;
  193. }
  194. .page-section {
  195. z-index: 0;
  196. }
  197. .store-tips {
  198. width: 600rpx;
  199. height: 100rpx;
  200. margin: 10rpx auto;
  201. border-radius: 10rpx;
  202. position: absolute;
  203. top: 10rpx;
  204. left: 50%;
  205. transform: translate(-50%, 0);
  206. z-index: 2;
  207. overflow: hidden;
  208. .store-des-box {
  209. background: #fff;
  210. }
  211. .store-img {
  212. width: 80rpx;
  213. height: 80rpx;
  214. border-radius: 10rpx;
  215. margin: 10rpx;
  216. float: left;
  217. }
  218. .store-des {
  219. padding-top: 8rpx;
  220. float: left;
  221. line-height: 1;
  222. font-size: 22rpx;
  223. color: #666;
  224. padding-left: 20rpx;
  225. .store-name {
  226. font-weight: 600;
  227. color: deeppink;
  228. }
  229. }
  230. .store-clear {
  231. width: 30rpx;
  232. height: 30rpx;
  233. position: absolute;
  234. top: 7rpx;
  235. right: 7rpx;
  236. margin: 30rpx;
  237. }
  238. }
  239. .address-icon {
  240. width: 38rpx;
  241. height: 40rpx;
  242. position: absolute;
  243. top: 22%;
  244. left: 50%;
  245. z-index: 2;
  246. margin-bottom: -20rpx;
  247. margin-left: -20rpx;
  248. }
  249. .near-num {
  250. padding: 10rpx 20rpx;
  251. border-radius: 10rpx;
  252. position: absolute;
  253. top: 17%;
  254. left: 50%;
  255. z-index: 2;
  256. font-size: 24rpx;
  257. background: #fff;
  258. transform: translate(-50%, 0);
  259. }
  260. </style>

生成高德地图Key

上述代码调试是可以定位到当前位置的,如需发布后仍需定位 则需要申请key

发布版安全码SHA1
应用打包时使用证书的指纹字符串(SHA1)
HBuilderX云端打包选择“使用自有证书”或本地离线打包时,填写应用使用证书的SHA1,查看证书的信息获取指纹中SHA1值,参考:https://ask.dcloud.net.cn/article/35777#keyinfo
HBuilderX云端打包选择“公共测试证书”时,SHA1值为:BB:AC:E2:2F:97:3B:18:02:E7:D6:69:A3:7A:28:EF:D2:3F:A3:68:E7,详情参考:https://ask.dcloud.net.cn/article/36522
HBuilderX云端打包选择“老版本DCloud公用证书”时,SHA1值为:BA:AD:09:3A:82:82:9F:B4:32:A7:B2:8C:B4:CC:F0:E9:F3:7D:AE:58,详情参考:https://ask.dcloud.net.cn/article/36522
注意:必须添写实际打包使用证书的SHA1值,否则可能无法显示地图或无法定位 

申请高德地图key时 PackageName为您打包时的包名

 

 

 

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

闽ICP备14008679号