googlemap.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div class="content">
  3. <div v-if="mapType == 'selectPoint'" style="display: flex;align-items: center;margin-bottom: 20px;">
  4. <div>{{$t('mendian.SearchAddress')}}:</div>
  5. <input
  6. id="pac-input"
  7. type="text"
  8. :placeholder="$t('mendian.Pleasekeywords')"
  9. />
  10. <div style="margin-left: 20px;">{{$t('mendian.currentlocation')}}:{{address}}</div>
  11. </div>
  12. <div :style="googleMapStyle" class="googleMap" :id="mapID"></div>
  13. </div>
  14. </template>
  15. <script>
  16. import {getfenlei,storelistlist,getAddr} from '@/api/store';
  17. import { Loader } from "@googlemaps/js-api-loader"
  18. export default {
  19. name:'googlemap',
  20. props: {
  21. //mapType属性值为:
  22. //selectPoint时是地图选点
  23. //POLYGON是绘制地图多边形区域
  24. //地图id
  25. mapID: {
  26. type: String,
  27. default: () => {
  28. return 'googleMap'
  29. },
  30. },
  31. //谷歌地图类型
  32. mapType: {
  33. type: String,
  34. default: () => {
  35. return 'default'
  36. },
  37. },
  38. //谷歌地图样式
  39. googleMapStyle: {
  40. type: Object,
  41. default: () => {
  42. return {
  43. wdith: '100%',
  44. height: '500px',
  45. }
  46. },
  47. },
  48. //谷歌地图配置
  49. mapOptions: {
  50. type: Object,
  51. default: () => {
  52. return {
  53. //为了关闭默认控件集,设置地图的disableDefaultUI的属性为true
  54. disableDefaultUI: false,
  55. // 启用缩放和平移
  56. gestureHandling: 'greedy',
  57. panControl: true,
  58. zoomControl: true,
  59. scaleControl: true,
  60. //关闭街景
  61. streetViewControl: false,
  62. }
  63. },
  64. },
  65. //谷歌地图中心点
  66. googleMapCenter: {
  67. type: Object,
  68. default: () => {
  69. return {
  70. lat: 21.031859020043132,
  71. lng:105.82760815495604
  72. }
  73. },
  74. },
  75. //谷歌地图缩放级别
  76. zoom: {
  77. type: Number,
  78. default() {
  79. return 11
  80. },
  81. },
  82. //谷歌地图图形path
  83. mapPath: {
  84. type: String,
  85. default: () => {
  86. return ''
  87. },
  88. },
  89. },
  90. data() {
  91. return {
  92. //地图选点回显值
  93. longlat: 21.031859020043132 + ',' + 105.82760815495604,
  94. //标记点
  95. marker: [],
  96. //图形实例
  97. graphicalExample: null,
  98. //图形路径经纬度
  99. graphicalPath: [],
  100. apiKey: 'AIzaSyBt-bYNprVqfpY3fPgoOj0RKbnC_g7ii1k', //旧的AIzaSyAnz_I0VUlPvt73NIocb_7tomJOW4CdwHk
  101. map:null,
  102. googMap:null,
  103. googMarker:null,
  104. address:''
  105. }
  106. },
  107. created() {
  108. this.initMap();
  109. },
  110. methods:{
  111. // initMap is now async
  112. async initMap() {
  113. var that = this;
  114. const loader = new Loader({
  115. apiKey: this.apiKey,
  116. version: "weekly",
  117. libraries: ['places', 'drawing'], //插件库places为基础库 drawing为绘制工具库
  118. language: this.$t('loding.language')
  119. });
  120. const mapOptions = {
  121. center: { lat: this.googleMapCenter.lat * 1, lng: this.googleMapCenter.lng * 1 }, //中心点
  122. zoom: this.zoom, //缩放级别
  123. mapId:this.mapID,
  124. ...this.mapOptions, //其他配置
  125. }
  126. await loader.load().then(async () => {
  127. this.googMap = await google.maps.importLibrary("maps");
  128. this.googMarker = await google.maps.importLibrary("marker");
  129. this.map = new this.googMap.Map(document.getElementById(this.mapID), mapOptions);
  130. this.map.addListener('click', (e) => {
  131. console.log('点击事件',e);
  132. this.marker.map = null;
  133. this.marker = new this.googMarker.AdvancedMarkerElement({
  134. map:this.map,
  135. position: { lat: e.latLng.lat(), lng: e.latLng.lng() },
  136. });
  137. })
  138. });
  139. },
  140. // 逆向地址
  141. // getdizhi(lat){
  142. // var user = JSON.parse(localStorage.getItem('user'));
  143. // var params={
  144. // latlng:lat,
  145. // id:user.id
  146. // }
  147. // getAddr(params).then(res=>{
  148. // this.address = res.data.address
  149. // this.$emit('golat',{lat:lat,address:res.data.address})
  150. // })
  151. // },
  152. // //回显图形
  153. // echoGraphical() {
  154. // if (this.mapType == 'POLYGON') {
  155. // //回显多边形
  156. // this.graphicalPath = this.mapPath
  157. // let paths = JSON.parse(this.graphicalPath)
  158. // // Construct a draggable red triangle with geodesic set to true.
  159. // this.graphicalExample = new this.googleApi.maps.Polygon({
  160. // paths,
  161. // fillColor: 'blue',
  162. // strokeColor: 'blue',
  163. // strokeOpacity: 0.8,
  164. // fillOpacity: 0.4,
  165. // strokeWeight: 5,
  166. // clickable: true,
  167. // editable: true,
  168. // zIndex: 1,
  169. // })
  170. // this.graphicalExample.setMap(this.googleMap)
  171. // //监听回显图形每一个路径点
  172. // this.googleApi.maps.event.addListener(this.graphicalExample.getPath(), 'insert_at', (event) => {
  173. // this.setPath(this.graphicalExample.getPath().Ld)
  174. // })
  175. // this.googleApi.maps.event.addListener(this.graphicalExample.getPath(), 'set_at', (event) => {
  176. // this.setPath(this.graphicalExample.getPath().Ld)
  177. // })
  178. // }
  179. // },
  180. // //path路径emit传出
  181. // setPath(path) {
  182. // this.graphicalPath = []
  183. // if (path.length > 0) {
  184. // path.forEach((v) => {
  185. // this.graphicalPath.push({ lat: v.lat(), lng: v.lng() })
  186. // })
  187. // this.$emit('getGraphicalPath', this.graphicalPath)
  188. // }
  189. // },
  190. // //搜索地点方法
  191. // selectPoint() {
  192. // const input = document.getElementById('pac-input')
  193. // const searchBox = new this.googleApi.maps.places.SearchBox(input)
  194. // const infoWindow = new this.googleApi.maps.InfoWindow()
  195. // // this.googleMap.controls[this.googleApi.maps.ControlPosition.TOP_LEFT].push(input)
  196. // this.googleMap.addListener('click', (e) => {
  197. // this.clickGmap(e)
  198. // })
  199. // this.googleMap.addListener('bounds_changed', () => {
  200. // searchBox.setBounds(this.googleMap.getBounds())
  201. // let markers = []
  202. // searchBox.addListener('places_changed', () => {
  203. // const places = searchBox.getPlaces()
  204. // if (places.length == 0) {
  205. // return
  206. // }
  207. // // Clear out the old markers.
  208. // markers.forEach((marker) => {
  209. // marker.setMap(null)
  210. // })
  211. // markers = []
  212. // // For each place, get the icon, name and location.
  213. // const bounds = new this.googleApi.maps.LatLngBounds()
  214. // places.forEach((place) => {
  215. // if (!place.geometry || !place.geometry.location) {
  216. // // console.log('Returned place contains no geometry')
  217. // return
  218. // }
  219. // const icon = {
  220. // url: place.icon,
  221. // size: new this.googleApi.maps.Size(71, 71),
  222. // origin: new this.googleApi.maps.Point(0, 0),
  223. // anchor: new this.googleApi.maps.Point(17, 34),
  224. // scaledSize: new this.googleApi.maps.Size(25, 25),
  225. // }
  226. // // Create a marker for each place.
  227. // markers.push(
  228. // new this.googleApi.maps.marker.AdvancedMarkerElement({
  229. // map: this.googleMap,
  230. // icon,
  231. // title: place.name,
  232. // position: place.geometry.location,
  233. // draggable: false,
  234. // title: 'Uluru'
  235. // })
  236. // )
  237. // if (place.geometry.viewport) {
  238. // // Only geocodes have viewport.
  239. // bounds.union(place.geometry.viewport)
  240. // } else {
  241. // bounds.extend(place.geometry.location)
  242. // }
  243. // })
  244. // this.googleMap.fitBounds(bounds)
  245. // })
  246. // })
  247. // },
  248. // //点击地图回调
  249. // clickGmap(e) {
  250. // console.log('点击回调',e)
  251. // this.longlat = e.latLng.lat() + ',' + e.latLng.lng()
  252. // this.$emit('setLongLat', this.longlat)
  253. // this.addDraggableMarkers(1, true, { lat: e.latLng.lat(), lng: e.latLng.lng() })
  254. // this.getdizhi(this.longlat);
  255. // },
  256. // //移动标记点回调
  257. // updateMaker(e) {
  258. // console.log('移动回调',e)
  259. // this.longlat = e.latLng.lat() + ',' + e.latLng.lng()
  260. // this.$emit('setLongLat', this.longlat)
  261. // },
  262. // //添加可拖动的标记点
  263. // addDraggableMarkers(max, draggable, { lat = 0, lng = 0 }) {
  264. // if (max != -1) {
  265. // this.marker.forEach((marker) => {
  266. // marker.setMap(null)
  267. // })
  268. // this.marker = []
  269. // }
  270. // this.marker.push(
  271. // new this.googleApi.maps.Marker({
  272. // map: this.googleMap,
  273. // position: { lat: lat, lng: lng },
  274. // draggable: draggable,
  275. // })
  276. // )
  277. // if (draggable) {
  278. // this.marker[0].addListener('dragend', (e) => {
  279. // this.updateMaker(e)
  280. // })
  281. // }
  282. // },
  283. //获取经纬度
  284. // getLongLat() {
  285. // return this.longlat
  286. // },
  287. //设置地图中心点位置
  288. // setMapPanBy(e) {
  289. // let latlng = new this.googleApi.maps.LatLng(e.lat,e.lng)
  290. // this.googleMap.setCenter(latlng)
  291. // this.marker[0].setPosition(latlng);
  292. // },
  293. }
  294. }
  295. </script>
  296. <style>
  297. </style>