8.微信小程序之地图demo

╰半夏微凉° 2022-05-05 09:30 357阅读 0赞

大佬让我调研一下微信小程序的地图怎么使用,写个一demo。

怎么感觉有好多坑。。。

一.效果图

1.用markers显示标记点,用callout自定义气泡弹窗,如图粉红框所示。点击标记点,可以打印信息;

2.随便拖动地图到任意位置,点击红色框中的内容可以返回初始位置,即“你所在的位置”

3.拖动结束时,获取地图中间的经纬度,控制台可看见信息

70

二.代码

1.map.wxml

  1. <map id="map"
  2. longitude="{
  3. {longitude}}"
  4. latitude="{
  5. {latitude}}"
  6. show-location
  7. scale='15'
  8. markers="{
  9. {markers}}"
  10. bindmarkertap="markertap"
  11. controls="{
  12. {controls}}"
  13. bindcontroltap="controltap"
  14. bindregionchange="regionchange"
  15. >
  16. </map>

2.map.wxss

  1. #map{
  2. width: 100%;
  3. height: 100vh;
  4. }

3.map.js

说一个注意点。markers是标记点用于在地图上显示标记的位置,用callout可以在标记点上显示气泡弹窗,气泡弹窗中的属性fontSize、borderRadius、padding不能是字符串,必须是数字,否则没效果。

目前,markers的数据是写死的,待完善。

  1. // pages/map/map.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. latitude: 0,
  9. longitude: 0,
  10. controls: [
  11. {
  12. id: 0,
  13. iconPath: "../../image/position.png",
  14. position: {
  15. left: 10,
  16. top: 400,
  17. width: 40,
  18. height: 40
  19. },
  20. clickable: true
  21. }
  22. ],
  23. markers: [
  24. {
  25. longitude: 108.31343,
  26. latitude: 22.83393,
  27. id: 0,
  28. width: 35,
  29. height: 30,
  30. callout: {
  31. content: "你所在的位置",
  32. color: "#ff0000",
  33. fontSize: 16,
  34. borderRadius: 10,
  35. bgColor: "#ffffff",
  36. padding: 10,
  37. display: "ALWAYS"
  38. }
  39. },
  40. {
  41. longitude: 108.28544,
  42. latitude: 22.83844,
  43. id: 0,
  44. width: 35,
  45. height: 30,
  46. callout: {
  47. content: "位置1",
  48. color: "#ff0000",
  49. fontSize: 16,
  50. borderRadius: 10,
  51. bgColor: "#ffffff",
  52. padding: 10,
  53. display: "ALWAYS"
  54. }
  55. },
  56. {
  57. longitude: 108.26073,
  58. latitude: 22.84558,
  59. id: 0,
  60. width: 35,
  61. height: 30,
  62. callout: {
  63. content: "位置2",
  64. color: "#ff0000",
  65. fontSize: 16,
  66. borderRadius: 10,
  67. bgColor: "#ffffff",
  68. padding: 10,
  69. display: "ALWAYS"
  70. }
  71. },
  72. {
  73. longitude: 108.324590,
  74. latitude: 22.842579,
  75. id: 0,
  76. width: 35,
  77. height: 30,
  78. callout: {
  79. content: "位置3",
  80. color: "#ff0000",
  81. fontSize: 16,
  82. borderRadius: 10,
  83. bgColor: "#ffffff",
  84. padding: 10,
  85. display: "ALWAYS"
  86. }
  87. },
  88. ]
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. var that = this;
  95. //获取当前位置
  96. wx.getLocation({
  97. success: function (res) {
  98. console.log(res);
  99. that.setData({
  100. longitude: res.longitude,
  101. latitude: res.latitude,
  102. })
  103. }
  104. })
  105. },
  106. /**
  107. * 生命周期函数--监听页面显示
  108. */
  109. onShow: function () {
  110. this.mapCtx = wx.createMapContext('map'); //获取地图存入变量
  111. },
  112. markertap(e) { //点击标记
  113. console.log(e);
  114. },
  115. regionchange(e) { //拖动地图
  116. if (e.type == 'end') { //如果拖动结束,也就是松开手指
  117. console.log('拖动结束,下面获取地图中间的经纬度');
  118. this.mapCtx.getCenterLocation({ //获取屏幕中间的经纬度
  119. success: function (res) {
  120. console.log(res)
  121. }
  122. })
  123. }
  124. },
  125. controltap(e) { //点击左下角的固定按钮
  126. console.log(e.controlId)
  127. if (e.controlId == 0) { //如果ID是0,返回当前位置
  128. this.mapCtx.moveToLocation()
  129. } else if (e.controlId == 1) {
  130. }
  131. },
  132. })

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序地图

    地图组件: 完成两大功能:A .显示地理位置;B .标记建筑物。 1、完成最简单的地图显示: 为通过CSS设置一个宽度和高度。 2、如何显示指定位置的地图: