Google Map V3:在输入搜索中定位清除X.

时间:2015-11-11 20:59:32

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

我正在尝试在输入上放置一个清除按钮以删除Google Map V3中的文本。

CSS:

#searchclear {
    //Doesn't matter what I put
} 

一般Javascript:

 var input = /** @type {HTMLInputElement} */(
      document.getElementById('pac-input'));
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    var searchclear = /** @type {HTMLInputElement} */(
      document.getElementById('searchclear'));

  map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchclear);

HTML:

<input id="pac-input" class="controls" type="text" placeholder="Search food, sites, places">                    
<span id="searchclear" class="glyphicon glyphicon-remove-circle" onclick="clearAllSearch()"></span>

我知道它与map.controls上的TOP_LEFT有关,但是如何更改它以便我可以在删除按钮上实际使用display:inline-block? 如果我删除下面的行,则删除按钮将被放置在地图之外。 map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchclear);

我可以使用绝对定位,但当浏览器窗口缩小或展开时,删除按钮将缩放和中断。

有什么建议吗?我试图将x放在输入中,在最右边。

TOP_LEFT问题: enter image description here

1 个答案:

答案 0 :(得分:2)

您可以考虑以下方法。

将图标和输入元素放在容器中:

window.alert

您可以使用以下CSS左右对齐字形:

<div id="search-container" class="inner-addon right-addon">
        <input id="pac-input" class="controls" type="text" placeholder="Enter a location">
        <span id="searchclear" class="glyphicon glyphicon-remove-circle" onclick="clearAllSearch()"></span>
</div>
  

最初此css已发布在this answer

最后将控件放在地图上:

/* enable absolute positioning */
.inner-addon { 
    position: relative; 
}

/* style icon */
.inner-addon .glyphicon {
  position: absolute;
  padding: 10px;
  pointer-events: none;
}

/* align icon */
.left-addon .glyphicon  { left:  0px;}
.right-addon .glyphicon { right: 0px;}

/* add padding  */
.left-addon input  { padding-left:  30px; }
.right-addon input { padding-right: 30px; }

<强>结果

enter image description here

工作示例

&#13;
&#13;
var searchContainer = document.getElementById('search-container');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchContainer);
&#13;
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: { lat: -33.8688, lng: 151.2195 },
        zoom: 13
    });
    
    var searchContainer = document.getElementById('search-container');
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchContainer);


    var input = document.getElementById('pac-input');
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });

    autocomplete.addListener('place_changed', function () {
        infowindow.close();
        marker.setVisible(false);
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            window.alert("Autocomplete's returned place contains no geometry");
            return;
        }

        // If the place has a geometry, then present it on a map.
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  
        }
        marker.setIcon(/** @type {google.maps.Icon} */({
            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(35, 35)
        }));
        marker.setPosition(place.geometry.location);
        marker.setVisible(true);

        var address = '';
        if (place.address_components) {
            address = [
              (place.address_components[0] && place.address_components[0].short_name || ''),
              (place.address_components[1] && place.address_components[1].short_name || ''),
              (place.address_components[2] && place.address_components[2].short_name || '')
            ].join(' ');
        }

        infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
        infowindow.open(map, marker);
    });
}

function clearAllSearch() {
    
}
&#13;
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#map {
    height: 100%;
}


.controls {
    margin-top: 10px;
    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 30px 0 13px;
    text-overflow: ellipsis;
    width: 300px;
}

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

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






.inner-addon {
    position: relative;
}

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

/* align icon */
.left-addon .glyphicon {
    left: 0px;
}

.right-addon .glyphicon {
    right: 0px;
}
&#13;
&#13;
&#13;

Plunker