Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Using google api location to auto complete the address
- Am using google location search api
- Am using two text box to autocomplete location
- for example in need enter origin and destination
- i can able to autocomplete only one text box at a time
- for first text box i can autocomplete and its working. Unable to do same
- <script>
- var placeSearch, autocompleter;
- var componentForm = {
- street_number: 'short_name',
- route: 'long_name',
- locality: 'long_name',
- administrative_area_level_1: 'short_name',
- country: 'long_name',
- postal_code: 'short_name'
- };
- function initAutocompletes() {
- autocompleter = new google.maps.places.Autocomplete(
- (document.getElementById('autocompleter')),
- {types: ['geocode']});
- autocompleter.addListener('place_changed', fillInAddress);
- }
- function fillInAddress() {
- var place = autocompleter.getPlace();
- for (var component in componentForm) {
- document.getElementById(component).value = '';
- document.getElementById(component).disabled = false;
- }
- for (var i = 0; i < place.address_components.length; i++) {
- var addressType = place.address_components[i].types[0];
- if (componentForm[addressType]) {
- var val = place.address_components[i][componentForm[addressType]];
- document.getElementById(addressType).value = val;
- }
- }
- }
- function geolocater() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var geolocation = {
- lat: position.coords.latitude,
- lng: position.coords.longitude
- };
- var circle = new google.maps.Circle({
- center: geolocation,
- radius: position.coords.accuracy
- });
- autocompleter.setBounds(circle.getBounds());
- });
- }
- }
- </script>
- <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB042hvI_n-AgJG2YpvxkUgYssoWZTdGT8&libraries=places&callback=initAutocomplete"async defer></script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment