googleMap.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view class="content">
  3. <view style="position: relative;" id="container">
  4. <view class="loader"></view>
  5. <view style="width: 100%; height: calc(50vh - 0rpx)" id="mapModule" :prop="nearCabinetList"
  6. :change:prop="mapModule.update" :change:markersIcon='mapModule.markersIcon'
  7. :change:markersIconActive='mapModule.markersIconActive' :markersIcon='markersIcon'
  8. :markersIconActive='markersIconActive' :lngLat="myLocation" :change:lngLat="mapModule.initAmap"></view>
  9. </view>
  10. </view>
  11. </template>
  12. <script module="mapModule" lang="renderjs">
  13. var common = require("@/common/common.js");
  14. var transform = require('../../common/transform.js');
  15. var meMarker = []
  16. var polylinePath = null
  17. export default {
  18. data() {
  19. return {
  20. infoWindow: {},
  21. selectedMarker: null,
  22. defaultIcon: {},
  23. selectedIcon: {},
  24. markerData: {},
  25. markersIcon: '',
  26. markersIconActive: '',
  27. ownerInstance: null,
  28. map: {},
  29. receiveTraceDate: []
  30. };
  31. },
  32. mounted() {
  33. },
  34. methods: {
  35. initAmap(lngLat, _, ownerInstance) {
  36. console.log('213123123123')
  37. console.log(_)
  38. lngLat = {
  39. lat: lngLat.latitude,
  40. lng: lngLat.longitude
  41. }
  42. lngLat = transform.wgs_gcj_encrypts(lngLat)
  43. // lngLat = transform.gcj_wgs_encrypts(lngLat)
  44. // console.log(lngLat, "坐标系lngLat")
  45. let _this = this
  46. // script.src ='https://map.gpsall.cc/maps/api/js?key=AIzaSyA70JUgslynjmVv0NPz3hcvkTbPZpr2i_c&callback=initMap&v=weekly'
  47. const script = document.createElement('script')
  48. script.src =
  49. 'https://map.gpsall.cc/maps/api/js?key=AIzaSyA70JUgslynjmVv0NPz3hcvkTbPZpr2i_c&callback=initMap&v=weekly&loading=async';
  50. script.async = true; // 异步加载
  51. script.defer = true; // 延迟执行
  52. document.head.appendChild(script)
  53. window.initMap = function() {
  54. _this.loadAmap(lngLat)
  55. };
  56. },
  57. loadPanTo(lngLat, _, ownerInstance) {
  58. lngLat = {
  59. lat: lngLat.latitude,
  60. lng: lngLat.longitude
  61. }
  62. console.log("lngLat", lngLat)
  63. lngLat = transform.wgs_gcj_encrypts(lngLat)
  64. console.log("lngLat", lngLat)
  65. this.map.panTo(new google.maps.LatLng(lngLat.latitude, lngLat.longitude))
  66. },
  67. loadAmap(lngLat) {
  68. let _this = this
  69. var myOptions = {
  70. center: {
  71. lat: parseFloat(lngLat.latitude),
  72. lng: parseFloat(lngLat.longitude)
  73. },
  74. gestureHandling: "greedy", // 强制单指拖动
  75. fullscreenControl: false,
  76. draggable: true, // 显式启用拖拽
  77. zoom: 14,
  78. panControl: false,
  79. zoomControl: false,
  80. scaleControl: false,
  81. streetViewControl: false,
  82. mapTypeControl: false,
  83. clickableIcons: false,
  84. }
  85. // 在初始化时定义图标配置(确保路径正确)
  86. _this.defaultIcon = {
  87. url: _this.markersIcon, // 默认图标路径
  88. size: new google.maps.Size(50, 52),
  89. scaledSize: new google.maps.Size(50, 52)
  90. };
  91. _this.selectedIcon = {
  92. url: _this.markersIconActive, // 选中图标路径
  93. size: new google.maps.Size(50, 52),
  94. scaledSize: new google.maps.Size(50, 52)
  95. };
  96. // 存储当前选中的 Marker
  97. _this.selectedMarker = null;
  98. _this.map = new google.maps.Map(document.getElementById("mapModule"), myOptions);
  99. //地图绑定事件
  100. new google.maps.event.addListener(_this.map, "click", function(event) {
  101. _this.$ownerInstance.callMethod('bindTapMap')
  102. });
  103. _this.addMarkers(_this.$ownerInstance)
  104. // console.log("render初始化数据")
  105. _this.$ownerInstance.callMethod('loadInitData')
  106. },
  107. addMarkers(ownerInstance) {
  108. this.Markers.map(item => {
  109. this.AddMarker(item, ownerInstance)
  110. })
  111. },
  112. AddMarker(item, ownerInstance) {
  113. let _this = this
  114. const marker = new google.maps.Marker({
  115. id: item.id,
  116. map: _this.map,
  117. data: item,
  118. position: new google.maps.LatLng(parseFloat(item.latitude), parseFloat(item.longitude)),
  119. icon: this.defaultIcon,
  120. zIndex: 20
  121. });
  122. marker.addListener('click', function(event) {
  123. if (_this.selectedMarker) {
  124. _this.selectedMarker.setIcon(_this.defaultIcon);
  125. _this.selectedMarker.setOptions({ label: null });
  126. }
  127. marker.setIcon(_this.selectedIcon);
  128. let distance = common.getFlatternDistance(_this.myLocation.longitude, _this.myLocation
  129. .latitude, item.longitude, item.latitude)
  130. let time = Math.ceil(Number(((distance - 0) / 1000).toFixed(2)) * 25 / 10)
  131. let content = `${common.formatDistance(Number(distance))} 骑行${time}分钟`
  132. marker.setOptions({
  133. label: {
  134. text: content,
  135. className: "map-label",
  136. }
  137. })
  138. _this.selectedMarker = marker;
  139. _this.loadMarkertap(marker)
  140. })
  141. },
  142. update(newValue, _, ownerInstance) {
  143. this.Markers = newValue
  144. },
  145. loadMarkertap(marker) {
  146. let markerId = marker.data.markerId
  147. if (markerId == 5000) return
  148. this.$ownerInstance.callMethod('onMarkertap',marker.data)
  149. },
  150. }
  151. }
  152. </script>
  153. <script>
  154. export default {
  155. props: {
  156. nearCabinetList: {
  157. type: Array,
  158. required: true //必填项
  159. },
  160. myLocation: {
  161. type: Object,
  162. required: true //必填项
  163. },
  164. markersIcon: {
  165. type: String,
  166. default: ""
  167. },
  168. markersIconActive: {
  169. type: String,
  170. default: ""
  171. },
  172. },
  173. data() {
  174. return {
  175. };
  176. },
  177. mounted() {},
  178. methods: {
  179. onMarkertap(marker) {
  180. this.$emit('changMarkertap',marker)
  181. },
  182. bindTapMap() {
  183. console.log('点击事件!')
  184. },
  185. loadInitData() {
  186. console.log('初始化成功!')
  187. },
  188. }
  189. }
  190. </script>
  191. <style>
  192. </style>