将控件(按钮)添加到谷歌地图搜索框

时间:2018-02-08 06:36:04

标签: javascript jquery html css google-maps

我正在尝试使用谷歌地图搜索框示例。我的代码基于这个例子。 https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

现在我想在搜索框后添加按钮。当我点击搜索框时,everthing工作正常。我想在按钮上触发它,也想在输入框后添加按钮。

HTML

<div class="search-box">
  <input id="pac-input" class="controls" type="text" placeholder="Search Box">
  <button>Search</button>
</div> 

这是我的代码:https://codepen.io/sifulislam/pen/qxqvZq

2 个答案:

答案 0 :(得分:4)

function initialize() {
if (typeof map == "undefined") { //this line prevents duplicate initializing
    var markers = [];
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-33.8902, 151.1759),
        new google.maps.LatLng(-33.8474, 151.2631));
    map.fitBounds(defaultBounds);

    var input = document.getElementById('pac-input');
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    var button = document.getElementById('doSearch');
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(button);
}

google.maps.event.addDomListener(window, 'load', initialize);

document.getElementById('pac-input').addEventListener("input", function () {
    $(".pac-container").remove();
    google.maps.event.clearInstanceListeners(document.getElementById('pac-input'));
});

document.getElementById('doSearch').onclick = function () {
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);

    google.maps.event.addListener(searchBox, 'places_changed', function () {

    });

    google.maps.event.addListener(map, 'bounds_changed', function () {
        var bounds = map.getBounds();
        searchBox.setBounds(bounds);
    });

    var places = searchBox.getPlaces();

    setTimeout(function () { $("#pac-input").focus() }, 100);

    if (!places || places.length == 0) {
        return;
    }
    for (var i = 0, marker; marker = markers[i]; i++) {
        marker.setMap(null);
    }

    markers = [];
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, place; place = places[i]; i++) {
        var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
        };

        var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
        });

        markers.push(marker);

        bounds.extend(place.geometry.location);
    }
    map.fitBounds(bounds);
}
}

$('#tvl-ways-tab a').on('click', function (e) {
    e.preventDefault();
    $(this).tab('show');
    if (this.text == "tab 1")
        initialize();  //Initialize map function
});

initialize();
#map-canvas {
    height: 500px;
    margin: 0px;
    padding: 0px
}
.pt {
margin-top: 20px;
}
.controls {
    margin-top: 16px;
    border: 1px solid transparent;
    border-radius: 2px 0 0 2px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    height: 32px;
    outline: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
    background-color: #fff;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    margin-left: 12px;
    padding: 0 11px 0 13px;
    text-overflow: ellipsis;
    width: 200px;
}

#pac-input:focus {
    border-color: #4d90fe;
}

.pac-container {
    font-family: Roboto;
}

#type-selector {
    color: #fff;
    background-color: #4d90fe;
    padding: 5px 11px 0px 11px;
}

#type-selector label {
    font-family: Roboto;
    font-size: 13px;
    font-weight: 300;
}
  
#doSearch {
    z-index: 0;
    position: absolute;
    left: 330px !important;
    top: 20px !important;
}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/popper.js@1.13.0/dist/umd/popper.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places&.js"></script>

<div class="tvl-ways-nav border-bottom">
                <ul class="nav container nav-pills" id="tvl-ways-tab" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" id="tvl-activities-tab" data-toggle="pill" href="#tvl-activities" role="tab" aria-controls="tvl-activities" aria-selected="true">tab 1</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="tvl-tour-tab" data-toggle="pill" href="#tvl-tour" role="tab" aria-controls="tvl-tour" aria-selected="false">tab 2</a>
                    </li>                
                    <li class="nav-item">
                        <a class="nav-link" id="tvl-hotel-tab" data-toggle="pill" href="#tvl-hotel" role="tab" aria-controls="tvl-hotel" aria-selected="false">tab 3</a>
                    </li>
                </ul>
            </div>
            <div class="tab-content tvl-tab-content mt-3" id="pills-tabContent">
                <div class="tab-pane fade show active" id="tvl-activities" role="tabpanel" aria-labelledby="tvl-activities-tab">
                    <section class="tvl-home-activity-search">
                        <div class="container pl-0 pr-0">
                            <div class="tvl-home-title section-title-small map-search">
                                <div class="tvl-home-search-inner border border-success">
                                    <div class="map-section">
                                        <div class="searchbox">
                                          <input id="pac-input" class="controls" type="text" placeholder="Country, Segment, Activity">  
                                        </div>
                                        <div  id="map-canvas"></div>
                                        <button id="doSearch" class="btn btn-fill map-btn">Search</button>
                                    </div>
                                </div>
                            </div>    
                        </div>
                    </section>
                </div>
                <div class="tab-pane fade" id="tvl-tour" role="tabpanel" aria-labelledby="tvl-tour-tab">
                    <div class="container">
                        <p>Tour Content Here</p>
                    </div>
                </div>
                <div class="tab-pane fade" id="tvl-hotel" role="tabpanel" aria-labelledby="tvl-hotel-tab">
                    <div class="container">
                        <p>Hotel Content Here</p>
                    </div>
                </div>
            </div>

更新:你可以添加你想要的任何控件:

HTML

<button id="doSearch">Search</button>

的Javascript

var button = document.getElementById('doSearch');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(button);

之后,您可以将事件绑定到按钮的onclick

document.getElementById('doSearch').onclick = function () {
    var input = document.getElementById('doSearch');

    google.maps.event.trigger(input, 'focus')
    google.maps.event.trigger(input, 'keydown', { keyCode: 13 });
}

Edited code in codepen.io

答案 1 :(得分:0)

对于布局,将ID添加到Button和控件类。

 <div class="container pt">
    <div class="search-box">
      <input id="pac-input" class="controls" type="text" placeholder="Search Box">
      <button class="controls" id="custom-button">Search</button>
    </div>
    <div  id="map-canvas"></div>
 </div>

添加以下样式

.search-box {
  z-index: 1;
  position: relative;
  left: 540px;
  top: 48px;
  height: 32px;
  width: 70px;
  margin: 0;
  display: inline-block;
}

.search-box button {
  margin: 0;
}

用于按钮的搜索功能。使用google maps api将按钮绑定到输入#pac-input的触发事件。

document.getElementById('custom-button').onclick = function () { google.maps.event.trigger(input, 'focus') google.maps.event.trigger(input, 'keydown', { keyCode: 13 }); };

有用的链接:Google Maps - Trigger Search Box on button click