googmap.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="content">
  3. <div style="display: flex;align-items: center;margin-bottom: 20px;">
  4. <div>{{$t('mendian.SearchAddress')}}:</div>
  5. <input
  6. type="text"
  7. v-model="ssneir"
  8. :placeholder="$t('mendian.Pleasekeywords')"
  9. @keyup.enter="selectPoint"
  10. />
  11. <button style="margin-left: 10px;" @click="selectPoint">{{$t('mendian.SearchAddress')}}</button>
  12. <div style="margin-left: 20px;">{{$t('mendian.currentlocation')}}:{{address}}</div>
  13. </div>
  14. <!-- 添加地址选择提示 -->
  15. <div v-if="isLoadingAddress" style="background-color: #f0f9ff; color: #1890ff; padding: 10px; margin-bottom: 10px; border-radius: 4px; display: flex; align-items: center;">
  16. <i class="el-icon-loading" style="margin-right: 8px;"></i>
  17. 正在获取地址信息...
  18. </div>
  19. <div v-else-if="!address" style="background-color: #fef0f0; color: #f56c6c; padding: 10px; margin-bottom: 10px; border-radius: 4px; display: flex; align-items: center;">
  20. <i class="el-icon-warning" style="margin-right: 8px;"></i>
  21. {{$t('mendian.Selectmapobtainaddress')}}
  22. </div>
  23. <div :style="googleMapStyle" class="googleMap" :id="mapID"></div>
  24. </div>
  25. </template>
  26. <script>
  27. import {getAddr} from '@/api/infouser/user';
  28. import { Loader } from '@googlemaps/js-api-loader' //引入
  29. export default {
  30. name:'googmap',
  31. props: {
  32. //selectPoint时是地图选点
  33. //POLYGON是绘制地图多边形区域
  34. //地图id
  35. mapID: {
  36. type: String,
  37. default: () => {
  38. return 'googleMap'
  39. },
  40. },
  41. //谷歌地图样式
  42. googleMapStyle: {
  43. type: Object,
  44. default: () => {
  45. return {
  46. wdith: '100%',
  47. height: '500px',
  48. }
  49. },
  50. },
  51. //谷歌地图配置
  52. mapOptions: {
  53. type: Object,
  54. default: () => {
  55. return {
  56. //为了关闭默认控件集,设置地图的disableDefaultUI的属性为true
  57. disableDefaultUI: false,
  58. // 启用缩放和平移
  59. gestureHandling: 'greedy',
  60. panControl: true,
  61. zoomControl: true,
  62. scaleControl: true,
  63. //关闭街景
  64. streetViewControl: false,
  65. }
  66. },
  67. },
  68. //谷歌地图中心点
  69. googleMapCenter: {
  70. type: Object,
  71. default: () => {
  72. return {
  73. lat: 21.031859020043132,
  74. lng:105.82760815495604
  75. }
  76. },
  77. },
  78. //谷歌地图缩放级别
  79. zoom: {
  80. type: Number,
  81. default() {
  82. return 11
  83. },
  84. },
  85. //谷歌地图图形path
  86. mapPath: {
  87. type: String,
  88. default: () => {
  89. return ''
  90. },
  91. },
  92. },
  93. data() {
  94. return {
  95. //地图选点回显值
  96. longlat: 21.031859020043132 + ',' + 105.82760815495604,
  97. //标记点
  98. marker: [],
  99. //图形实例
  100. graphicalExample: null,
  101. //图形路径经纬度
  102. graphicalPath: [],
  103. apiKey: 'AIzaSyDRpbm3HCpmUjfaPu3TgklBGwWJWrvIXDw', //旧的AIzaSyAnz_I0VUlPvt73NIocb_7tomJOW4CdwHk
  104. googleMap:null,
  105. address:'',
  106. ssneir:'',
  107. isLoadingAddress: false
  108. }
  109. },
  110. created() {
  111. this.$nextTick(() => {
  112. const loader = new Loader({
  113. apiKey: this.apiKey, //之前的key
  114. version: 'weekly', //版本
  115. libraries: ['places', 'drawing'], //插件库places为基础库 drawing为绘制工具库
  116. region: 'Canada',
  117. language: this.$t('loding.language'),
  118. })
  119. const mapOptions = {
  120. center: { lat: this.googleMapCenter.lat * 1, lng: this.googleMapCenter.lng * 1 }, //中心点
  121. zoom: this.zoom, //缩放级别
  122. mapId:this.mapID,
  123. ...this.mapOptions, //其他配置
  124. }
  125. loader.load().then(async (google) => {
  126. this.googMap = await google.maps.importLibrary("maps");
  127. this.googMarker = await google.maps.importLibrary("marker");
  128. const map = new this.googMap.Map(document.getElementById(this.mapID), mapOptions);
  129. this.googleMap = map;
  130. this.googleApi = google;
  131. this.googleMap.addListener('click', (e) => {
  132. this.clickGmap(e)
  133. })
  134. }).catch((e) => {
  135. // do something
  136. // console.log(e)
  137. })
  138. })
  139. },
  140. methods:{
  141. // 逆向地址
  142. getdizhi(lat){
  143. this.isLoadingAddress = true;
  144. // id参数用于语言设置,不是用户ID,可以传空字符串或任意值
  145. var params={
  146. latlng:lat,
  147. id:'' // 空字符串即可,后端会使用默认语言
  148. }
  149. console.log('调用getAddr API,参数:', params);
  150. getAddr(params).then(res=>{
  151. console.log('地址API响应:',res)
  152. this.address = res.data.address
  153. this.$emit('golat',{lat:lat,address:res.data.address})
  154. this.isLoadingAddress = false;
  155. }).catch(error => {
  156. console.error('获取地址失败:', error);
  157. // 即使API失败,也要触发事件,使用默认地址
  158. this.$emit('golat',{lat:lat,address:'地址获取失败'})
  159. this.isLoadingAddress = false;
  160. })
  161. },
  162. //搜索地点方法
  163. selectPoint() {
  164. if (!this.ssneir) {
  165. this.$message.warning('请输入搜索关键词');
  166. return;
  167. }
  168. if (!this.googleApi || !this.googleMap) return;
  169. const service = new this.googleApi.maps.places.PlacesService(this.googleMap);
  170. service.textSearch({ query: this.ssneir }, (results, status) => {
  171. if (status === this.googleApi.maps.places.PlacesServiceStatus.OK && results.length > 0) {
  172. const loc = results[0].geometry.location;
  173. const lat = loc.lat();
  174. const lng = loc.lng();
  175. this.googleMap.setCenter({ lat, lng });
  176. this.googleMap.setZoom(15);
  177. if (this.marker) this.marker.map = null;
  178. this.marker = new this.googMarker.AdvancedMarkerElement({
  179. map: this.googleMap,
  180. position: { lat, lng },
  181. });
  182. this.getdizhi(lat + ',' + lng);
  183. } else {
  184. this.$message.error('搜索失败,请重试');
  185. }
  186. });
  187. },
  188. //点击地图回调
  189. clickGmap(e) {
  190. console.log('点击回调',e)
  191. this.longlat = e.latLng.lat() + ',' + e.latLng.lng();
  192. console.log('设置坐标:', this.longlat);
  193. this.marker.map = null;
  194. this.marker = new this.googMarker.AdvancedMarkerElement({
  195. map:this.googleMap,
  196. position: { lat: e.latLng.lat(), lng: e.latLng.lng() },
  197. });
  198. console.log('开始调用getdizhi方法');
  199. this.getdizhi(this.longlat);
  200. },
  201. //获取经纬度
  202. // getLongLat() {
  203. // return this.longlat
  204. // },
  205. //设置地图中心点位置
  206. // setMapPanBy(e) {
  207. // let latlng = new this.googleApi.maps.LatLng(e.lat,e.lng)
  208. // this.googleMap.setCenter(latlng)
  209. // this.marker[0].setPosition(latlng);
  210. // },
  211. }
  212. }
  213. </script>
  214. <style>
  215. </style>