簡體   English   中英

在 Modal 中具有自動完成功能的 Google 地圖搜索框

[英]Google Maps Search Box with autocomplete in Modal

是否可以在模式中使用自動完成功能的 Google 地圖搜索框? 不是整個地圖,只是搜索框。

像這樣的東西:

在此處輸入圖片說明

幾個小時后,我似乎找到了一種方法:)

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>

在此處輸入圖片說明 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM