jquery - Facing error on Maps API v3 Places Autocomplete Form -
i using maps api v3 places autocomplete form last few month project when adding page places autocomplete not working.
i getting error on line:
for (var component in componentform) { document.getelementbyid(component).value = ''; document.getelementbyid(component).disabled = false; } and getting error message: typeerror: document.getelementbyid(...) null
here autocomplete code:
<script type='text/javascript'> $(function(){ // example displays address form, using autocomplete feature // of google places api users fill in information. var placesearch, autocomplete; 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 initialize() { // create autocomplete object, restricting search // geographical location types. autocomplete = new google.maps.places.autocomplete( /** @type {htmlinputelement} */ (document.getelementbyid('autocomplete')), { //types: ['geocode'] types: ['geocode', 'establishment'] }); // when user selects address dropdown, // populate address fields in form. google.maps.event.addlistener(autocomplete, 'place_changed', function () { fillinaddress(); }); } // [start region_fillform] function fillinaddress() { // place details autocomplete object. var place = autocomplete.getplace(); var formatted_address = place.formatted_address; (var component in componentform) { document.getelementbyid(component).value = ''; document.getelementbyid(component).disabled = false; } // each component of address place details // , fill corresponding field on form. (var = 0; < 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; } } } // [end region_fillform] // [start region_geolocation] // bias autocomplete object user's geographical location, // supplied browser's 'navigator.geolocation' object. function geolocate() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(function (position) { var geolocation = new google.maps.latlng( position.coords.latitude, position.coords.longitude); autocomplete.setbounds(new google.maps.latlngbounds(geolocation, geolocation)); }); } } initialize(); // [end region_geolocation] }); </script> my jsfiddle: http://jsfiddle.net/mananpatel/ls2ag2p0/
any idea? why not working.
thanks.
i think you're getting null pointer when attempting assign country. try adding in field country, such
<div class="form-group"> <label class="control-label">country</label> <input type="text" name="country" id="country" value="" placeholder="" class="form-control" maxlength="15"/> </div> when added jsfiddle submitted, worked correctly.
Comments
Post a Comment