简体   繁体   中英

Google map autocomplete search box not working

I am trying to add a search box to autocomplete that will make the google map go to the location in the search box. So far not having much luck figuring out why it is not working.

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

Your code is working, only issue is with the input element you get, you have used # tag to get input, here is your working code,

Your Code

need to change is,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM