mapindex.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <view class="content">
  3. <map id="map" class="map" :show-location="true" :latitude="latitude" :longitude="longitude"></map>
  4. </view>
  5. </template>
  6. <script>
  7. const img = '/static/logo.png';
  8. export default {
  9. data() {
  10. return {
  11. latitude: 43.780458,
  12. longitude: 125.384426,
  13. }
  14. },
  15. onReady() {
  16. this._mapContext = uni.createMapContext("map", this);
  17. // 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
  18. this._mapContext.initMarkerCluster({
  19. enableDefaultStyle: false,
  20. zoomOnClick: true,
  21. gridSize: 60,
  22. complete(res) {
  23. console.log('initMarkerCluster', res)
  24. }
  25. });
  26. this._mapContext.on("markerClusterCreate", (e) => {
  27. console.log("markerClusterCreate", e);
  28. });
  29. this.addMarkers();
  30. },
  31. methods: {
  32. addMarkers() {
  33. const positions = [
  34. {
  35. latitude: 43.780458,
  36. longitude: 125.384426,
  37. }
  38. ]
  39. const markers = []
  40. positions.forEach((p, i) => {
  41. console.log(i)
  42. markers.push(
  43. Object.assign({},{
  44. id: i + 1,
  45. iconPath: img,
  46. width: 50,
  47. height: 100,
  48. joinCluster: true, // 指定了该参数才会参与聚合
  49. label: {
  50. width: 50,
  51. height: 30,
  52. borderWidth: 1,
  53. borderRadius: 10,
  54. bgColor: '#ffffff',
  55. content: `定位点`
  56. }
  57. },p)
  58. )
  59. })
  60. this._mapContext.addMarkers({
  61. markers,
  62. clear: false,
  63. complete(res) {
  64. console.log('addMarkers', res)
  65. }
  66. })
  67. }
  68. }
  69. }
  70. </script>
  71. <style>
  72. .content {
  73. flex: 1;
  74. }
  75. .map {
  76. width: 20rem;
  77. height: 40rem;
  78. flex: 1;
  79. }
  80. </style>