Ben_hawk
Ben_hawk

Reputation: 2486

Google Maps API Auto Complete

I am attempting use Google Maps API to create two auto complete input boxes along with a map to mark the positions.

Currently based on the examples in the Docs I have got a single input box which works with auto-complete and places a marker on the map, however I would now like to expand on this and have a second input box with does the same thing on the same map, but having trouble achieving it.

The code so far is

<script type="text/javascript">
        function initialize() {
                var mapOptions = {
                  center: new google.maps.LatLng(-33.8688, 151.2195),
                  zoom: 13,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById('map'),
                  mapOptions);

                var input = document.getElementById('frombox');
                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
                });


                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                  infowindow.close();
                  var place = autocomplete.getPlace();
                  if (place.geometry.viewport) {
                    map.fitBounds(place.geometry.viewport);
                  } else {
                    map.setCenter(place.geometry.location);
                    map.setZoom(15);
                  }


                  var image = new google.maps.MarkerImage(
                      place.icon,
                      new google.maps.Size(71, 71),
                      new google.maps.Point(0, 0),
                      new google.maps.Point(17, 34),
                      new google.maps.Size(35, 35));
                  marker.setIcon(image);
                  marker.setPosition(place.geometry.location);

                  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);

                });



                // Sets a listener on a radio button to change the filter type on Places
                // Autocomplete.
                function setupClickListener(id, types) {
                  var radioButton = document.getElementById(id);
                  google.maps.event.addDomListener(radioButton, 'click', function() {
                    autocomplete.setTypes(types);
                  });
                }

                setupClickListener('changetype-all', []);
                setupClickListener('changetype-establishment', ['establishment']);
                setupClickListener('changetype-geocode', ['geocode']);
              }
              google.maps.event.addDomListener(window, 'load', initialize);                 

                        </script>

Could anyone help me set up the second input box called #tobox to do the same thing on the same map, I would really appreciate it.

Thanks

Upvotes: 1

Views: 2175

Answers (1)

cgs
cgs

Reputation: 11

You probably already figured out how to do this, so I'm going to post in case someone else has a similar issue.

using your code, you would need to create a new reference to another input element, a new autocomplete object and add to the listener list:

var input2 = document.getElementById('frombox2');
var autocomplete2 = new google.maps.places.Autocomplete(input2);
autocomplete2.bindTo('bounds', map);
google.maps.event.addListener(autocomplete2, 'place_changed' function (){...});

I would recomend removing the anonymous function and declaring one with a name so that you can reference in the 2nd search box function.

Thanks

Upvotes: 1

Related Questions