home.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. if (!Array) {
  4. const _component_van_button = common_vendor.resolveComponent("van-button");
  5. _component_van_button();
  6. }
  7. const _sfc_main = {
  8. __name: "home",
  9. setup(__props) {
  10. const GUANGZHOU_CENTER = {
  11. latitude: 23.12911,
  12. longitude: 113.26436
  13. };
  14. const GUANGZHOU_POIS = [
  15. { id: 1, name: "广州塔", latitude: 23.10641, longitude: 113.32466 },
  16. { id: 2, name: "白云山", latitude: 23.19746, longitude: 113.30249 },
  17. { id: 3, name: "越秀公园", latitude: 23.13927, longitude: 113.26436 },
  18. { id: 4, name: "沙面岛", latitude: 23.10788, longitude: 113.24365 },
  19. { id: 5, name: "陈家祠", latitude: 23.12632, longitude: 113.24849 },
  20. { id: 6, name: "北京路", latitude: 23.12389, longitude: 113.26799 },
  21. { id: 7, name: "上下九", latitude: 23.11696, longitude: 113.24899 },
  22. { id: 8, name: "荔枝博览园", latitude: 22.99405, longitude: 113.32486 },
  23. { id: 9, name: "中山纪念堂", latitude: 23.13146, longitude: 113.26336 },
  24. { id: 10, name: "海心沙", latitude: 23.11446, longitude: 113.32136 }
  25. ];
  26. const qqmapsdk = common_vendor.ref(null);
  27. const mapContext = common_vendor.ref(null);
  28. const center = common_vendor.ref(GUANGZHOU_CENTER);
  29. const allMarkers = common_vendor.ref([]);
  30. const clusters = common_vendor.ref([]);
  31. const includePoints = common_vendor.ref([]);
  32. const currentZoom = common_vendor.ref(16);
  33. const initMap = () => {
  34. qqmapsdk.value = new common_vendor.QQMapWX({
  35. key: "Q5GBZ-2LP6I-LOKGM-UE3UC-TXH7Z-WCFG2"
  36. // key
  37. });
  38. };
  39. const loadPoints = () => {
  40. allMarkers.value = GUANGZHOU_POIS.map((item) => ({
  41. ...item,
  42. iconPath: "../../../static/map/point.png",
  43. width: 30,
  44. height: 30,
  45. callout: {
  46. content: item.name,
  47. color: "#ffffff",
  48. bgColor: "#007AFF",
  49. padding: 5,
  50. borderRadius: 4,
  51. display: "ALWAYS"
  52. }
  53. }));
  54. updateClusters();
  55. };
  56. const updateClusters = () => {
  57. if (currentZoom.value >= 15) {
  58. clusters.value = allMarkers.value;
  59. } else {
  60. const clusterRadius = 60 / currentZoom.value;
  61. const clustered = [];
  62. allMarkers.value.forEach((marker) => {
  63. let isClustered = false;
  64. clustered.forEach((cluster) => {
  65. const distance = getDistance(
  66. cluster.latitude,
  67. cluster.longitude,
  68. marker.latitude,
  69. marker.longitude
  70. );
  71. if (distance < clusterRadius) {
  72. isClustered = true;
  73. cluster.markers.push(marker);
  74. cluster.callout.content = `${cluster.markers.length}个地点`;
  75. }
  76. });
  77. if (!isClustered) {
  78. clustered.push({
  79. ...marker,
  80. markers: [marker],
  81. callout: {
  82. ...marker.callout,
  83. content: marker.name
  84. }
  85. });
  86. }
  87. });
  88. clusters.value = clustered.map((cluster) => ({
  89. id: cluster.id,
  90. latitude: cluster.latitude,
  91. longitude: cluster.longitude,
  92. iconPath: cluster.markers.length > 1 ? "../../../static/map/point.png" : "../../../static/map/point.png",
  93. width: cluster.markers.length > 1 ? 40 : 30,
  94. height: cluster.markers.length > 1 ? 40 : 30,
  95. callout: cluster.callout,
  96. clusterData: cluster.markers
  97. }));
  98. }
  99. includePoints.value = allMarkers.value.map((m) => ({
  100. latitude: m.latitude,
  101. longitude: m.longitude
  102. }));
  103. };
  104. const getDistance = (lat1, lng1, lat2, lng2) => {
  105. const toRad = (d) => d * Math.PI / 180;
  106. const R = 6371;
  107. const dLat = toRad(lat2 - lat1);
  108. const dLng = toRad(lng2 - lng1);
  109. const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
  110. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  111. return R * c * 1e3;
  112. };
  113. const onRegionChange = (e) => {
  114. if (e.type === "end") {
  115. common_vendor.index.createMapContext("myMap").getScale({
  116. success: (res) => {
  117. currentZoom.value = res.scale;
  118. updateClusters();
  119. }
  120. });
  121. }
  122. };
  123. common_vendor.onMounted(() => {
  124. mapContext.value = common_vendor.index.createMapContext("myMap", this);
  125. initMap();
  126. loadPoints();
  127. if (mapContext.value && mapContext.value.getRegion) {
  128. mapContext.value.getRegion({
  129. success: (res) => common_vendor.index.__f__("log", "at pages/tabBar/home/home.vue:270", "地图范围:", res),
  130. fail: (err) => common_vendor.index.__f__("error", "at pages/tabBar/home/home.vue:271", "地图异常:", err)
  131. });
  132. }
  133. });
  134. return (_ctx, _cache) => {
  135. return {
  136. a: center.value.latitude,
  137. b: center.value.longitude,
  138. c: clusters.value,
  139. d: [],
  140. e: includePoints.value,
  141. f: common_vendor.o(onRegionChange),
  142. g: common_vendor.p({
  143. type: "primary"
  144. }),
  145. h: _ctx.duration
  146. };
  147. };
  148. }
  149. };
  150. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-15dac74a"]]);
  151. wx.createPage(MiniProgramPage);
  152. //# sourceMappingURL=../../../../.sourcemap/mp-weixin/pages/tabBar/home/home.js.map