amap.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="amap-container">
  3. <view :prop="markerList" :change:prop="amap.updateEcharts" id="amap"></view>
  4. <view class="">当前点击的对象的index值为:{{ dataIndex }}</view>
  5. </view>
  6. </template>
  7. <script>
  8. const start = 'static/ITkoala-amap/start.png'
  9. export default {
  10. data() {
  11. return {
  12. markerList: [],
  13. dataIndex: ''
  14. }
  15. },
  16. mounted() {
  17. this.$nextTick(() => {
  18. this.getMapData()
  19. })
  20. },
  21. methods: {
  22. // 模拟从后台获取地图数据
  23. getMapData() {
  24. this.markerList = [
  25. {
  26. lat: 39.908775,
  27. lng: 116.406315,
  28. icon: start
  29. },
  30. {
  31. lat: 39.973253,
  32. lng: 116.473195,
  33. icon: start
  34. },
  35. {
  36. lat: 39.953253,
  37. lng: 116.453195,
  38. icon: start
  39. }
  40. ]
  41. },
  42. //地图点击回调事件
  43. onViewClick(params) {
  44. this.dataIndex = params.dataIndex
  45. }
  46. }
  47. }
  48. </script>
  49. <script module="amap" lang="renderjs">
  50. import config from './config.js'
  51. const selectedStart = 'static/ITkoala-amap/selectedStart.png' //选中的图片
  52. export default {
  53. data() {
  54. return {
  55. map: null,
  56. ownerInstanceObj: null //service层对象
  57. }
  58. },
  59. mounted() {
  60. if (typeof window.AMap === 'function') {
  61. this.initAmap()
  62. } else {
  63. // 动态引入较大类库避免影响页面展示
  64. const script = document.createElement('script')
  65. script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=' + config.WEBAK
  66. script.onload = this.initAmap.bind(this)
  67. document.head.appendChild(script)
  68. }
  69. },
  70. methods: {
  71. initAmap() {
  72. this.map = new AMap.Map('amap', {
  73. resizeEnable: true,
  74. center: [116.397428, 39.90923],
  75. layers: [ //使用多个图层
  76. // new AMap.TileLayer.Satellite() //使用卫星图
  77. ],
  78. zooms: [4, 18], //设置地图级别范围
  79. zoom: 10
  80. })
  81. this.initMarkers()
  82. },
  83. //初始化标记点
  84. initMarkers() {
  85. let prevMarker = null
  86. let prevIcon = null
  87. this.markerList.forEach((item, index) => {
  88. if(!!item.icon){
  89. //添加点标记
  90. let marker = new AMap.Marker({
  91. position: new AMap.LngLat(item.lng, item.lat),
  92. offset: new AMap.Pixel(-13, -30),
  93. icon: item.icon
  94. })
  95. marker.on('click', (e) => {
  96. if(!!prevMarker){
  97. prevMarker.setIcon(prevIcon)
  98. }
  99. prevIcon = item.icon
  100. prevMarker = marker
  101. marker.setIcon(selectedStart)
  102. this.dataIndex = index
  103. this.onClick(null,this.ownerInstanceObj)
  104. })
  105. this.map.add(marker)
  106. }
  107. })
  108. },
  109. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  110. // 监听 service 层数据变更
  111. this.ownerInstanceObj = ownerInstance
  112. },
  113. onClick(event, ownerInstance) {
  114. // 调用 service 层的方法
  115. ownerInstance.callMethod('onViewClick', {
  116. dataIndex: this.dataIndex
  117. })
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. #amap {
  124. width: 100%;
  125. height: 600rpx;
  126. }
  127. .infoWindow-wrap{
  128. margin-left: 50px;
  129. color: #f00;
  130. }
  131. </style>