当前位置:   article > 正文

在Vue中使用谷歌地图与vue-google-maps实现地图搜索、地图显示、坐标拾取及定位等功能_vue2-google-maps

vue2-google-maps

 

 

Google Maps 提供了个强大的 API 集用于构建地图并与地图交互、可视化位置数据以及通过自动完成进行搜索。vue-google-maps已针对 Vue.js 2 进行了更新,为 Vue.js 应用程序提供了利用这些 API 的工具。它带有一套完善的组件,用于与谷歌地图交互以及双向数据绑定。

首先,您需要安装vue2-google-maps:
npm install vue2-google-maps@0.10.7

 

在代码编辑器中打开src/main.js,导入vue2-google-maps并使用您的 API 密钥实例化插件: src/main.js 
  1. import Vue from 'vue'
  2. import * as VueGoogleMaps from 'vue2-google-maps'
  3. import App from './App.vue'
  4. Vue.config.productionTip = false
  5. Vue.use(VueGoogleMaps, {
  6. load: {
  7. key: '您的实际 API 密钥',
  8. libraries: 'places',
  9. }
  10. });
  11. new Vue({
  12. render: h => h(App),
  13. }).$mount('#app')
构建地图搜索组件
  1. <GmapAutocomplete @place_changed="setPlace" :placeholder='$t("googlemap.placeholder")' />
  2. <button class="add-btn cursor" @click="addMarker">{{ $t("allCable.search") }}</button>
 构建地图画布组件:
  1. <GmapMap
  2. @click="clickMap"
  3. :center="center"
  4. :zoom="zoom"
  5. style="width: 100%; height: 400px"
  6. >
  7. <GmapMarker
  8. :key="index"
  9. v-for="(m, index) in markers"
  10. :position="m.position"
  11. @click="clickMarker(m.position)"
  12. />
  13. </GmapMap>
 完整代码(功能包含地图搜索,点击地图拾取坐标,获取当前定位):
  1. <template>
  2. <div class="map">
  3. <el-row
  4. style="display: flex; justify-content: center; padding-bottom: 7vh"
  5. class=""
  6. >
  7. <el-col :xs="20" :sm="20" :md="16" :lg="15" :xl="14" style="">
  8. <div class="search-box">
  9. <h2 style="margin-bottom: 10px">
  10. {{ $t("googlemap.searchAndAdd") }}
  11. </h2>
  12. <GmapAutocomplete @place_changed="setPlace" :placeholder='$t("googlemap.placeholder")' />
  13. <button class="add-btn cursor" @click="addMarker">
  14. {{ $t("allCable.search") }}
  15. </button>
  16. </div>
  17. <br />
  18. <div class="GmapMapBox">
  19. <GmapMap
  20. @click="clickMap"
  21. :center="center"
  22. :zoom="zoom"
  23. style="width: 100%; height: 400px"
  24. >
  25. <GmapMarker
  26. :key="index"
  27. v-for="(m, index) in markers"
  28. :position="m.position"
  29. @click="clickMarker(m.position)"
  30. />
  31. </GmapMap>
  32. </div>
  33. <el-row style="" class="btn-box">
  34. <el-button type="primary" @click="submitMap">{{
  35. $t("form.sure")
  36. }}</el-button>
  37. <el-button @click="cancelMap">{{ $t("form.cancel") }}</el-button>
  38. </el-row>
  39. </el-col>
  40. </el-row>
  41. </div>
  42. </template>
  43. <script>
  44. export default {
  45. name: "GoogleMap",
  46. data() {
  47. return {
  48. center: { lat: 10.0, lng: 10.0 }, // 中心位置
  49. center_: { lat: 10.0, lng: 10.0 }, // 保存当前点位置
  50. currentPlace: null,
  51. markers: [],
  52. places: [],
  53. placeName: "",
  54. dialogVisible: true,
  55. googlemap: "",
  56. hasSetPin:false,
  57. icon:require("@/assets/img/loading.gif"),
  58. zoom:1
  59. };
  60. },
  61. props:['gpsName'],
  62. mounted() {
  63. if(this.gpsName!='Positioning'){
  64. let gpsArr = this.gpsName.split(',')
  65. if(gpsArr&&gpsArr.length>0){
  66. this.zoom = 10
  67. this.center.lat = gpsArr[0]*1
  68. this.center.lng = gpsArr[1]*1
  69. this.center_.lat = gpsArr[0]*1
  70. this.center_.lng = gpsArr[1]*1
  71. this.markers.push({ position: this.center_ });
  72. }
  73. this.hasSetPin = true
  74. } else {
  75. this.geolocate();
  76. }
  77. },
  78. methods: {
  79. handleClose() {},
  80. setPlace(place) {
  81. this.currentPlace = place;
  82. this.addMarkerFun();
  83. },
  84. addMarker() {
  85. this.addMarkerFun();
  86. },
  87. addMarkerFun(){
  88. if (this.currentPlace) {
  89. this.hasSetPin = true
  90. this.zoom = 10
  91. const marker = {
  92. lat: this.currentPlace.geometry.location.lat(),
  93. lng: this.currentPlace.geometry.location.lng(),
  94. };
  95. this.markers = [];
  96. this.markers.push({ position: marker });
  97. this.places.push(this.currentPlace);
  98. this.center = marker;
  99. this.center_ = marker;
  100. this.placeName = this.currentPlace.name;
  101. this.currentPlace = null;
  102. }
  103. },
  104. geolocate: function () {
  105. let vm = this
  106. if(navigator.geolocation){
  107. navigator.geolocation.getCurrentPosition((position) => {
  108. if(position && position.coords && position.coords.latitude){
  109. // alert("获取地理位置:"+position.coords.latitude+","+position.coords.longitude)
  110. vm.hasSetPin = true
  111. vm.zoom = 10
  112. vm.center = {
  113. lat: position.coords.latitude,
  114. lng: position.coords.longitude,
  115. };
  116. vm.center_ = vm.center
  117. vm.markers.push({ position: vm.center });
  118. }
  119. },(error)=>{ // html5 默认调用的谷歌的接口,会有安全限制
  120. switch(error.code)
  121. {
  122. case error.PERMISSION_DENIED: // 许可拒绝,用户选了不允许
  123. // alert("您拒绝对获取地理位置的请求");
  124. // alert(error.message);
  125. break;
  126. case error.POSITION_UNAVAILABLE: // 连不上GPS卫星,或者网络断了
  127. // alert("位置信息是不可用的");
  128. // alert(error.message);
  129. break;
  130. case error.TIMEOUT: // /超时了
  131. // alert("请求您的地理位置超时");
  132. // alert(error.message);
  133. break;
  134. case error.UNKNOWN_ERROR:
  135. // alert("未知错误");
  136. // alert(error.message);
  137. break;
  138. }
  139. });
  140. } else {
  141. // alert("navigator.geolocation未获取获取到地理位置");
  142. // vm.markers.push({ position: vm.center });
  143. }
  144. },
  145. clickMap(e) {
  146. this.hasSetPin = true
  147. let longlat = e.latLng.lat() + "," + e.latLng.lng();
  148. this.center_ = {
  149. lat: e.latLng.lat(),
  150. lng: e.latLng.lng(),
  151. };
  152. this.markers = [];
  153. this.markers.push({ position: this.center_ });
  154. },
  155. clickMarker(val) {
  156. this.center = val;
  157. },
  158. submitMap() {
  159. if(!this.hasSetPin){
  160. this.msgError(this.$t("googlemap.searchAndAdd"));
  161. return
  162. }
  163. let obj = Object.assign({}, this.center_);
  164. obj.name = `${this.center_.lat.toFixed(5)},${this.center_.lng.toFixed(5)}`;
  165. this.$emit("setMap", obj);
  166. },
  167. cancelMap() {
  168. this.$emit("closeMap");
  169. },
  170. },
  171. };
  172. </script>
  173. <style scoped>
  174. .map {
  175. /* height: 85vh; */
  176. padding-top: 10vh;
  177. }
  178. .add-btn {
  179. width: 60px;
  180. margin-left: 2%;
  181. height: 40px;
  182. background: #409eff;
  183. color: #fff;
  184. border: 0;
  185. border-radius: 5px;
  186. }
  187. .search-box input {
  188. height: 40px;
  189. width: 50%;
  190. border-radius: 5px;
  191. border: 1px solid #ccc;
  192. padding-left: 7px;
  193. outline: none;
  194. }
  195. /* 底部两个按钮 */
  196. .btn-box {
  197. margin-top: 2vh;
  198. }
  199. .btn-box .el-button {
  200. padding: 8px 30px;
  201. border-radius: 30px;
  202. width: 140px;
  203. height: 40px;
  204. }
  205. .confirm {
  206. color: #fff;
  207. background-color: #0778bc;
  208. }
  209. .cancel {
  210. color: #0778bc;
  211. border: 1px solid #0778bc;
  212. }
  213. .cursor {
  214. cursor: pointer;
  215. }
  216. @media only screen and (max-width: 820px) {
  217. .img-box .el-image {
  218. height: 4vh;
  219. }
  220. .search-box input {
  221. height: 5vh;
  222. }
  223. .add-btn {
  224. height: 5vh;
  225. }
  226. }
  227. .map .GmapMapBox{
  228. background-image: url(../assets/img/loading.gif);
  229. background-position: center center;
  230. background-repeat: no-repeat;
  231. background-size: 24px 24px;
  232. }
  233. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/215867
推荐阅读
相关标签
  

闽ICP备14008679号