February 24, 201214 yr 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