0

I need to display the Google Map after I have performed my search, but the search box should have autocomplete locations/places. The search box should have the autocomplete places while searching and when clicked on THEN the map should be displayed with the location accordingly.

Here is the code:

<script>

        function initAutocomplete() {
            var map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: -33.8688, lng: 151.2195},
                zoom: 13,
                mapTypeId: 'roadmap'
            });

            // Create the search box and link it to the UI element.
            var input = document.getElementById('pac-input');
            var searchBox = new google.maps.places.SearchBox(input);
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

            // Bias the SearchBox results towards current map's viewport.
            map.addListener('bounds_changed', function () {
                searchBox.setBounds(map.getBounds());
            });

            var markers = [];
            // Listen for the event fired when the user selects a prediction and retrieve
            // more details for that place.
            searchBox.addListener('places_changed', function () {
                var places = searchBox.getPlaces();

                if (places.length == 0) {
                    return;
                }

                // Clear out the old markers.
                markers.forEach(function (marker) {
                    marker.setMap(null);
                });
                markers = [];

                // For each place, get the icon, name and location.
                var bounds = new google.maps.LatLngBounds();
                places.forEach(function (place) {
                    if (!place.geometry) {
                        console.log("Returned place contains no geometry");
                        return;
                    }
                    var 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(25, 25)
                    };

                    // Create a marker for each place.
                    markers.push(new google.maps.Marker({
                        map: map,
                        icon: icon,
                        title: place.name,
                        position: place.geometry.location
                    }));

                    if (place.geometry.viewport) {
                        // Only geocodes have viewport.
                        bounds.union(place.geometry.viewport);
                    } else {
                        bounds.extend(place.geometry.location);
                    }
                });
                map.fitBounds(bounds);
            });
        }

</script>

<script src="https://maps.googleapis.com/maps/api/js?key
&libraries=places&callback=initAutocomplete"
        async defer></script>

And this is the other code:

<input id="pac-input" class="controls" type="text" placeholder="Search" >
<div id="map"></div> 
Iavor
  • 1,997
  • 16
  • 27
dalmatian
  • 19
  • 3

1 Answers1

1

One approach would be to set the visibility property of the <div id="map"></div> element to hidden in your CSS.

#map {
  height: 100%;
  visibility: hidden;
}

Then, at the end of your search box places_changed event handler, set the map element's visibility to visible:

document.getElementById('map').style.visibility = 'visible';

Also, in order to prevent the search box from going invisible as well, I would remove this line from your code:

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

Here is a JSBin with a working example.

Iavor
  • 1,997
  • 16
  • 27
  • Thank you so much @lavor . Is there a way to display the map in a different page? – dalmatian May 02 '18 at 14:50
  • Do you want to display the map after a page refresh or on a new tab? – Iavor May 02 '18 at 14:59
  • @lavor new tab, a new .php page – dalmatian May 02 '18 at 15:10
  • I'm not too sure; take a look at [this post](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) and [this one](https://stackoverflow.com/questions/16834138/javascript-function-post-and-call-php-script). It also might help to open up a new Stack Overflow question and ask there. More people will be able to help you that way. Also, don't forget to upvote or [accept](https://stackoverflow.com/help/accepted-answer) my answer if it helped you :-) – Iavor May 02 '18 at 15:24