【问题标题】:Google Maps Search Box with autocomplete in Modal谷歌地图搜索框在模态中自动完成
【发布时间】:2020-12-12 05:51:42
【问题描述】:

是否可以在模式中使用带有自动完成功能的 Google 地图搜索框? 不是整个地图,只是搜索框。

类似这样的:

【问题讨论】:

    标签: javascript html css google-maps google-maps-api-3


    【解决方案1】:

    几小时后,我似乎找到了一种方法:)

    HTML:

    <div class="modal fade" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-md" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <div class="md-form ml-0 mr-0">
                        <!-- Autocomplete location search input -->
                        <div class="form-group">
                            <label>Τοποθεσία:</label>
                            <input type="text" class="form-control" id="search_input" placeholder="Αναζήτηση διεύθυνσης..." />
                            <input type="hidden" id="loc_lat" />
                            <input type="hidden" id="loc_long" />
                        </div>
                        <!-- Display latitude and longitude -->
                        <div class="latlong-view">
                            <p style="text-align: center;">
                                <b>Latitude:</b>
                                <span id="latitude_view"></span>
                                <b>| Longitude:</b>
                                <span id="longitude_view"></span>
                            </p>
                        </div>
                    </div>
                    <div class="text-center mt-4">
                        <button class="btn btn-cyan waves-effect waves-light" onclick="goTo()" style=" font-size: 1.2rem;">
                            <i class="fa fa-search ml-1"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    

    Javascript:

    /* Google Maps Search handler */
    var searchInput = 'search_input';
    $(document).ready(function() {
        var autocomplete;
        autocomplete = new google.maps.places.Autocomplete((document.getElementById(searchInput)), {
            types: ['geocode'],
            componentRestrictions: {
                country: "GR"
            }
        });
    
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var near_place = autocomplete.getPlace();
            document.getElementById('loc_lat').value = near_place.geometry.location.lat();
            document.getElementById('loc_long').value = near_place.geometry.location.lng();
    
            document.getElementById('latitude_view').innerHTML = near_place.geometry.location.lat();
            document.getElementById('longitude_view').innerHTML = near_place.geometry.location.lng();
        });
    });
    
    $(document).on('change', '#' + searchInput, function() {
        document.getElementById('latitude_view').innerHTML = '';
        document.getElementById('longitude_view').innerHTML = '';
    });
    
    /* Google Maps - Go to searched location */
    function goTo() {
        var lat = Number(document.getElementById("latitude_view").innerText);
        var lon = Number(document.getElementById("longitude_view").innerText);
    
        console.log(lat);
        console.log(lon);
        if (lat != 0 && lon != 0) {
            map.setCenter({
                lat: lat,
                lng: lon
            });
            map.setZoom(18);
        } else
            alert("Μη αποδεκτές συντεταγμένες");
    }
    /* END of Google Maps Search handler */
    

    请注意,您必须使用地点库才能使其正常工作。

    <script defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places&callback=initMap"></script>
    

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 2012-11-21
      • 2011-09-19
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 2018-09-17
      相关资源
      最近更新 更多