谷歌地图搜索功能的bug

1、当页面结构复杂,具体原因不知道,主要会导致pac-container 搜索列表容器跑到body,不会显示在input下,解决方案有一个:手动强制修改

<div class="map_container" style="height:100%;">
        <div class="input_box">
            <input id="searchInput" ref="searchInput" v-model="searchKey" type="text" />
        </div>
        <div id="gmap" ref="gmap" style="height:100%;"></div>
        <div id="infowindow-content">
            <span id="place-name" class="title"></span><br />
            <span id="place-address"></span>
        </div>
    </div>
 mounted () {
   
        var addressInputElement = document.querySelector('#searchInput');
        addressInputElement.addEventListener('focus', function () {
   
            var pacContainer = document.querySelector('.pac-container');
            addressInputElement.parentNode.appendChild(pacContainer);
        });
     }
//scss语法
::v-deep .pac-container {
     
  z-index: 9999999 !important;  
  top: 30px !important;  
  left: 0 !important; 
}

以下是谷歌地图的搜索功能的js代码

          const mapDiv = document.getElementById('gmap');
          const map = new google.maps.Map(mapDiv, option);
          let options = {
   
                fields: ['ALL'],//搜索类型 :"formatted_address", "geometry", "name" 
                strictBounds: false,//严格模式
            };
            const input = document.getElementById('searchInput');
            console.log(input, 'this.optioninputinput');
            const autocomplete = new google.maps.places.Autocomplete(input, options);
            console.log(autocomplete, ' this.autocomplete');
            autocomplete.bindTo("bounds", map);

            //设置搜索地址功能
            const infowindow = new google.maps.InfoWindow();
            const infowindowContent = document.getElementById('infowindow-content');
            infowindow.setContent(infowindowContent);
            const marker = new google.maps.Marker({
   
                map: map,
                anchorPoint: new google.maps.Point(0, -29),
            });

            autocomplete.addListener("place_changed", () => {
   

                infowindow.close();
                marker.setVisible(false);
                const place = autocomplete.getPlace();
                console.log('触发搜索组件', place);

                if (!place.geometry || !place.geometry.location) {
   
                    window.alert("No details available for input: '" + place.name + "'");
                    return;
                }
                if (place.geometry.viewport) {
   
                    map.fitBounds(place.geometry.viewport);
                } else {
   
                    map.setCenter(place.geometry.location);
                    map.setZoom(17);
                }

                marker.setPosition(place.geometry.location);
                marker.setVisible(true);
                infowindowContent.children["place-name"].textContent = place.name;
                infowindowContent.children["place-address"].textContent = place.formatted_address;
                infowindow.open(map, marker);
            });

相关推荐

  1. 地图搜索功能bug

    2024-01-05 16:20:04       62 阅读
  2. 搜索语法

    2024-01-05 16:20:04       39 阅读
  3. 【js文件】地图 markerclusterer.js

    2024-01-05 16:20:04       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 16:20:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 16:20:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 16:20:04       82 阅读
  4. Python语言-面向对象

    2024-01-05 16:20:04       91 阅读

热门阅读

  1. hyper-V的虚拟磁盘扩容

    2024-01-05 16:20:04       57 阅读
  2. Typescript基础知识:函数类型和箭头函数

    2024-01-05 16:20:04       47 阅读
  3. chip-seq测序分析流程

    2024-01-05 16:20:04       59 阅读
  4. 【LeetCode】1164. 指定日期的产品价格

    2024-01-05 16:20:04       60 阅读
  5. mysql:SQL按时间查询方法总结

    2024-01-05 16:20:04       54 阅读
  6. centos7安装docker(包含yum配置阿里云镜像源)

    2024-01-05 16:20:04       62 阅读
  7. 解决.gitignore文件无效问题

    2024-01-05 16:20:04       55 阅读
  8. ffmpeg转码新技能

    2024-01-05 16:20:04       80 阅读
  9. Ajax同步调用影响加载动画展示问题

    2024-01-05 16:20:04       57 阅读
  10. anylabeling 加载模型后出错

    2024-01-05 16:20:04       69 阅读
  11. 偌依 项目部署及上线步骤

    2024-01-05 16:20:04       93 阅读
  12. 解析:Eureka的工作原理

    2024-01-05 16:20:04       54 阅读
  13. Eureka工作原理

    2024-01-05 16:20:04       50 阅读
  14. Eureka工作原理超详细讲解介绍

    2024-01-05 16:20:04       50 阅读
  15. Eureka工作原理

    2024-01-05 16:20:04       51 阅读