Google Geocode JavaScript Address Validation

I got a chance to play around with address validation using Google geocode API v3. In my case I use the client side geocoding technique to submit full address (street address + suburb + state + postcode) to find out whether the results returned. The suburb, state and postcode is coming from a proper list.



What I found out with Google geocode is that when you submit full address, it also returns result with partial_match and they don't give an easy filter to exclude these results. Since I will always submit a full string address, I will just get the address when there is no partial_match property in the returned results. So far it works quite well for my situation.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function ValidateContactAddress() {
    var contactAddress = $('txtContactStreetName').val().trim() + " " + $("#txtContactAusPostLocality").val();
    var isValid = IsValidAddress(contactAddress, ContactAddressValid, ContactAddressInvalid);
}
 
function ContactAddressValid(formattedAddress) {
    $('#txtContactStreetName').val(formattedAddress.substring(0, formattedAddress.indexOf(',')));
    alert('Valid');
}
 
function ContactAddressInvalid(address) {
    alert('Google Maps had some trouble finding ' + address);
}
 
function IsValidAddress(address, callbackValid, callbackNotValid) {
    var r;
 
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            for (var i = 0; i < results.length; i++) {
                if (results[i].partial_match == null) {
                    callbackValid(results[i].formatted_address);
                }
                else {
                    callbackNotValid(address);
                }
            }
 
        } else {
            callbackNotValid(address);
        }
    });
}

HTH,
Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline