mercredi 31 décembre 2014

Address validation with Google Geocode API


I have worked out getting part of my code to work properly but I am stumbling on getting the second stage of it to work. With the code below I am performing a US ZIP code validation through Google Geocode API and if valid I am auto populating a form field with the assigned city for that zip code. But I am trying to to get the function to run recursively or however else it needs to run to perform another validation against the street address entered into the form field. If it validates then nothing should happen, if the street address doesn't validate then user is presented an alert box just like the zip/state alert box on fault.


It seems that when Google cannot validate a numeric address but other information pushed to the API is verifiable, Google tries to respond with the closest approximation and the ITEM TYPE in the JSON response is "route" (Which is what I want to use to evaluate a non resolvable address) - a successful response returns a item type of "street_number"


How can I get the numeric address verification portion of the code to work?


HTML (Code minimized)



<?php include ('addverify.php'); ?>

<div style='margin-top:5px;' class='rt'><label> Service Address: </label><input id='saddr' name='saddr' type='text' class='rounded' style='width:230px' required placeholder='Numeric and street name'></div>
<div style='margin-top:5px;' class='rt'><label> Service City: </label><input id='scity' name='scity' type='text' class='rounded' style='width:230px' required></div>
<div style='margin-top:5px;' class='rt'><label> Service State: </label><select id='sstate' name='sstate' class='rounded' style='width:230px' required>
<option value='AL'>Alabama</option>
<option value='AK'>Alaska</option>
</select></div>
<div style='margin-top:5px;' class='rt'><label> Service Zip: </label><input id='szip' name='szip' type='text' maxlength='10' class='rounded' style='width:230px' required placeholder='Service Zip Code' onBlur='codeAddress()'></div>


addverfy.php (Javascript inclusion)



<script src='http://ift.tt/1lrRXYd'></script>
<script>
var geocoder;
function codeAddress() {
geocoder = new google.maps.Geocoder();
var address = document.getElementById('szip').value;
//var address = encodeURIcomponent(document.getElementById('saddr').value);
alert (address)
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {

for (var component in results[0]['address_components']) {
for (var i in results[0]['address_components'][component]['types']) {
//if (results[0]['address_components'][component]['types'][i] == 'route') { alert ('Unable to validate street address');return;}
if (results[0]['address_components'][component]['types'][i] == 'country') {
country = results[0]['address_components'][component]['long_name'];
if (country == 'United States') { document.getElementById('scity').value=results[0]['address_components'][1]['long_name'];} else {alert('Non US Zipcode');}
}
if (results[0]['address_components'][component]['types'][i] == 'administrative_area_level_1') {
state = results[0]['address_components'][component]['short_name'];
document.getElementById('sstate').value= state;
//alert (document.getElementById('sstate').value)
}

}
}


} else { alert('Data error with Google, try again'); }
});
}

</script>


Valid address resolution JSON response


Unresolvable address resolution JSON response





Aucun commentaire:

Enregistrer un commentaire