infowindow.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="amap-container">
  3. <view :prop="markerList" :change:prop="amap.updateEcharts" id="amap"></view>
  4. <view style="display: none;" id="infoWindow">
  5. <view class="infoWindow-wrap">
  6. <view class="infoWindow-content">
  7. <text class="infoWindow-text">当前点击的对象的index值为:{{ dataIndex }}</text>
  8. <image class="close" src="/static/ITkoala-amap/close.png" mode="widthFix"></image>
  9. </view>
  10. <view class="sharp">
  11. <image src="/static/ITkoala-amap/sharp.png" mode="widthFix"></image>
  12. </view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. const start = 'static/ITkoala-amap/start.png'
  19. export default {
  20. data() {
  21. return {
  22. markerList: [],
  23. dataIndex: ''
  24. }
  25. },
  26. mounted() {
  27. this.$nextTick(() => {
  28. this.getMapData()
  29. })
  30. },
  31. methods: {
  32. // 模拟从后台获取地图数据
  33. getMapData() {
  34. this.markerList = [
  35. {
  36. lat: 39.908775,
  37. lng: 116.406315,
  38. icon: start
  39. },
  40. {
  41. lat: 39.973253,
  42. lng: 116.473195,
  43. icon: start
  44. },
  45. {
  46. lat: 39.953253,
  47. lng: 116.453195,
  48. icon: start
  49. }
  50. ]
  51. },
  52. //地图点击回调事件
  53. onViewClick(params) {
  54. this.dataIndex = params.dataIndex
  55. }
  56. }
  57. }
  58. </script>
  59. <script module="amap" lang="renderjs">
  60. import config from './config.js'
  61. const selectedStart = 'static/ITkoala-amap/selectedStart.png' //选中的图片
  62. export default {
  63. data() {
  64. return {
  65. map: null,
  66. ownerInstanceObj: null, //service层对象
  67. currentItem: null //当前点击的对象
  68. }
  69. },
  70. mounted() {
  71. if (typeof window.AMap === 'function') {
  72. this.initAmap()
  73. } else {
  74. // 动态引入较大类库避免影响页面展示
  75. const script = document.createElement('script')
  76. script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=' + config.WEBAK
  77. script.onload = this.initAmap.bind(this)
  78. document.head.appendChild(script)
  79. }
  80. },
  81. methods: {
  82. initAmap() {
  83. this.map = new AMap.Map('amap', {
  84. resizeEnable: true,
  85. center: [116.397428, 39.90923],
  86. zooms: [4, 18], //设置地图级别范围
  87. zoom: 10
  88. })
  89. this.initMarkers()
  90. },
  91. //初始化标记点
  92. initMarkers() {
  93. let prevMarker = null
  94. let prevIcon = null
  95. this.markerList.forEach((item, index) => {
  96. if(!!item.icon){
  97. //添加点标记
  98. let marker = new AMap.Marker({
  99. position: new AMap.LngLat(item.lng, item.lat),
  100. offset: new AMap.Pixel(-13, -30),
  101. icon: item.icon
  102. })
  103. marker.on('click', (e) => {
  104. this.currentItem = item
  105. if(!!prevMarker){
  106. prevMarker.setIcon(prevIcon)
  107. }
  108. prevIcon = item.icon
  109. prevMarker = marker
  110. marker.setIcon(selectedStart)
  111. this.dataIndex = index
  112. this.onClick(null,this.ownerInstanceObj)
  113. setTimeout(() => {
  114. this.showInfoWindow()
  115. },100)
  116. })
  117. this.map.add(marker)
  118. }
  119. })
  120. },
  121. //显示信息窗体
  122. showInfoWindow(){
  123. let element = document.getElementById('infoWindow')
  124. let content = element.innerHTML
  125. let infoWindow = new AMap.InfoWindow({
  126. isCustom: true, //使用自定义窗体
  127. content: this.createInfoWindow(content),
  128. offset: new AMap.Pixel(16, -45)
  129. })
  130. infoWindow.open(this.map, new AMap.LngLat(this.currentItem.lng, this.currentItem.lat))
  131. },
  132. //构建自定义信息窗体
  133. createInfoWindow(content) {
  134. let info = document.createElement('div')
  135. info.innerHTML = content
  136. info.onclick = (ev) => {
  137. let target = (ev.target && ev.target.currentSrc) || null
  138. if(!!target && target.includes('close.png')){
  139. this.map.clearInfoWindow()
  140. }
  141. }
  142. /* 使用官方的创建关闭按钮 */
  143. /* let closeX = document.createElement("img")
  144. closeX.src = "https://webapi.amap.com/images/close2.gif"
  145. closeX.style.position = "absolute"
  146. closeX.style.right = '5px'
  147. closeX.style.top = '5px'
  148. closeX.onclick = () => {
  149. this.map.clearInfoWindow()
  150. }
  151. info.appendChild(closeX) */
  152. return info
  153. },
  154. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  155. // 监听 service 层数据变更
  156. this.ownerInstanceObj = ownerInstance
  157. },
  158. onClick(event, ownerInstance) {
  159. // 调用 service 层的方法
  160. ownerInstance.callMethod('onViewClick', {
  161. dataIndex: this.dataIndex
  162. })
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. #amap {
  169. width: 100%;
  170. height: 600rpx;
  171. }
  172. .infoWindow-wrap {
  173. position: relative;
  174. background: #fff;
  175. .infoWindow-content{
  176. padding: 30rpx;
  177. .infoWindow-text {
  178. color: #f00;
  179. }
  180. .close {
  181. width: 32rpx;
  182. height: 32rpx;
  183. position: absolute;
  184. top: -25rpx;
  185. right: -15rpx;
  186. }
  187. }
  188. .sharp{
  189. width: 30rpx;
  190. height: 23rpx;
  191. position: absolute;
  192. bottom: -23rpx;
  193. left: 0;
  194. right: 0;
  195. margin: auto;
  196. image{
  197. width: 100%;
  198. height: 100%;
  199. vertical-align: top;
  200. }
  201. }
  202. }
  203. </style>