Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Google Maps API

Featured Replies

Hi,

 

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 <strong>same map</strong>, 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

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.