当前位置:   article > 正文

Vue使用Google Map_vue 谷歌地图中英文

vue 谷歌地图中英文

一、使用前提条件

1、谷歌开发者账号

2、账号要绑定借记卡用来后续api的收费,(好像不支持大陆借记卡)

二、新增项目,添加引用的API

 三、凭据里面创建凭据,获得api的key

 四、vue安装依赖并初始化

npm install @googlemaps/js-api-loader
  1. <template>
  2. <!--要给盒子设置宽高 -->
  3. <div id="map" />
  4. </template>
  5. <script>
  6. import { Loader } from "@googlemaps/js-api-loader"
  7. const loader = new Loader({
  8. apiKey: "apikey", //api的key
  9. version: "weekly", //版本
  10. libraries: ["places"],
  11. })
  12. export default {
  13. data() {
  14. return {
  15. google: '',
  16. map: '',
  17. service: '',
  18. infoWindow: '',
  19. marker: ''
  20. }
  21. },
  22. methods: {
  23. // 地图初始化
  24. initMap() {
  25. const mapOptions = {
  26. center: { lat: 22.602, lng: 114.043 },
  27. zoom: 6
  28. }
  29. loader
  30. .load()
  31. .then((google) => {
  32. this.google = google
  33. this.map = new google.maps.Map(
  34. document.getElementById("map"),
  35. mapOptions
  36. )
  37. // service 地点查询类
  38. this.service = new google.maps.places.PlacesService(this.map)
  39. this.infoWindow = new google.maps.InfoWindow({ // 地图信息窗口
  40. content: "",
  41. // disableAutoPan: true,
  42. })
  43. this.marker = new google.maps.Marker() // 地图标记类
  44. this.google.maps.event.addListener(this.map, 'click', this.clickMap) // 监听地图点击事件
  45. }).catch((e) => {
  46. console.log(e)
  47. })
  48. },
  49. }
  50. }
  51. </script>

五、遇到的问题

1、怎么控制缩放zoom的数值大小以自动展示地图查询地点的详情:比如我搜索加拿大地图是展示这个国家的领土大小,我搜索加拿大Canada某个街道地图是放大到能展示街道具体的建筑物,缩放等级的控制问题让我找了半天的api

  1. this.marker.setMap(null) // 清除之前的标记
  2. const request = {
  3. query: '深圳市福田区平安大厦', // 搜索文本
  4. fields: ['name', 'geometry', 'place_id'], // 指定要返回的数据字段, 也可以写成 fields: ['all']
  5. }
  6. this.service.textSearch(request, (results, status) => {
  7. if (status === this.google.maps.places.PlacesServiceStatus.OK) {
  8. console.log('search', results[0])
  9. let res = results[0]
  10. this.marker = new this.google.maps.Marker({ // 给搜索地点添加地图标记
  11. position: res.geometry.location,
  12. map: this.map,
  13. })
  14. this.map.setCenter(this.marker.getPosition()) // 以搜索点为中心
  15. let myLatLngBounds = new this.google.maps.LatLngBounds(res.geometry.viewport) // 当前地点的几何视口
  16. this.map.fitBounds(myLatLngBounds) // 设置视口,使其包含指定边界。改变zoom缩放级别
  17. this.marker.addListener("click", (e) => { // 给标记添加点击事件
  18. this.infoWindow.setContent(res.name);
  19. this.infoWindow.open(this.map, this.marker);
  20. this.map.setZoom(this.map.getZoom() + 1)
  21. })
  22. if(res.place_id) resolve(res.place_id)
  23. }
  24. })

https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngBounds
google.maps.LatLngBounds 类 LatLngBounds实例代表以地理坐标表示的矩形,包括与 180 度子午线相交的矩形。

 

 

 2、搜索到地点后怎么取到国家、城市、区县、街道的值
Geocoding Service | Maps JavaScript API | Google Developers

  • country 表示国家/地区政治实体,通常是地理编码器返回的最高顺序类型。
  • administrative_area_level_1 表示国家/地区级别以下的一级行政实体。在美国,这类行政级别是指州。并非所有国家/地区都有这些行政级别。在大多数情况下,admin_area_level_1 简称与 ISO 3166-2 子类及其他广泛传播的名单非常接近;不过,这种实现方式无法保证,因为我们的地理编码结果基于各种信号和位置数据。
  • administrative_area_level_2 表示国家/地区级别下的二级行政实体。在美国,这类行政级别是指县。并非所有国家/地区都有这些行政级别。
  • administrative_area_level_3 表示国家/地区级别下的三级行政实体。此类型表示较小的行政部门。并非所有国家都设有这类行政级别
  1. getDetailsInfo(placeId) {
  2. const request = {
  3. placeId,
  4. fields: ['all']
  5. }
  6. this.service.getDetails(request, (res, status) => {
  7. if (status === this.google.maps.places.PlacesServiceStatus.OK) {
  8. console.log('details', res)
  9. let addressList = res.address_components, street = res.formatted_address.split(' ')
  10. let i = 0;
  11. for(i; i < addressList.length; i++) {
  12. if (addressList[i].types.includes('country')) this.region.country = addressList[i].long_name
  13. if (addressList[i].types.includes('administrative_area_level_1')) this.region.province = addressList[i].long_name
  14. if (addressList[i].types.includes('locality')) this.region.city = addressList[i].long_name
  15. else if (addressList[i].types.includes('sublocality_level_1')) this.region.city = addressList[i].long_name
  16. }
  17. }
  18. })
  19. },

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

闽ICP备14008679号