Google地图自动填充搜索框无效

时间:2017-04-25 01:49:03

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

我正在尝试添加一个搜索框来自动完成,这将使Google地图转到搜索框中的位置。到目前为止,没有太多的运气搞清楚它为什么不起作用。

---------- HTML

<div class="input-group location" >
         <input type="text" id="location" placeholder="Enter Location">
         <span class="input-group-addon"><i class="fa fa-map-marker geolocation" data-toggle="tooltip" data-placement="bottom" title="Find my position"></i></span>
</div>

----------的JavaScript

    var input = document.getElementById('#location') ;
    var autocomplete = new google.maps.places.Autocomplete(input, {
        types: ["geocode"]
    });

    autocomplete.bindTo('bounds', map);
    google.maps.event.addListener(autocomplete, 'place_changed', function() 
    {
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            return;
        }
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
            map.setZoom(14);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(14);
        }

        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(' ');
        }
    });

1 个答案:

答案 0 :(得分:2)

您的代码正常运行,唯一问题是您获得的输入元素,您使用#tag获取输入,这是您的工作代码,

Connectivity plugin

需要改变是,

var input = document.getElementById('location'); //删除#tag