Harmony RightAddress Autocomplete JavaScript client API

The client library acts as an extension of Harmony RightAddress JavaScript client. This is a lightweight vanilla JavaScript for auto-complete using the current Address lookup methods.

Please refer to the Code snippet section to copy and paste to your web page.

For JavaScript wrapper for auto-complete with jQuery and jQuery UI, please refer to JavaScript UI client API.

Methods

Name Description Since
HarmonyJS.addField Adds a new field specifying the field name (e.g. Harmony.POSTCODE, Harmony.LOCALITY, etc) or the attribute name (e.g. "attributes.Line1", "attributes.Latitude", etc), and the input element. 2.0.0
HarmonyJS.addressLookup Single-line address lookup on the supplied input element. 2.0.0

HarmonyJS.addField(field, element, index)

Adds a new field specifying the field name (e.g. Harmony.POSTCODE, Harmony.LOCALITY, etc) or the attribute name (e.g. "attributes.Line1", "attributes.Latitude", etc), and the input element.

field

Type: String The name of the field (e.g. Harmony.POSTCODE) or attribute (e.g. "attributes.Line1") to be added.

element

Type: Object The input element to be assigned to the field (e.g. document.getElementById("postcodeField")).

index

Type: Integer The optional index if there are multiple sets of lookups on the same page (e.g. one for residential addresses and the other one for postal addresses). Currently only available for addressLookup.

The following example assigns the Harmony.LOCALITY field to the input element with localityField ID and the "attributes.Latitude" attribute to the input element with latitudeField ID:


    // Perform the assignment using standard JavaScript on single address lookup.
    HarmonyJS.addField(Harmony.LOCALITY, document.getElementById("localityField"));
    HarmonyJS.addField("attributes.Latitude", document.getElementById("latitudeField"));
    
    // Perform the assignment using standard JavaScript on the second set address lookup.
    HarmonyJS.addField(Harmony.LOCALITY, document.getElementById("localityField"), 1);
    HarmonyJS.addField("attributes.Latitude", document.getElementById("latitudeField"), 1);
    

HarmonyJS.addressLookup

(element, countryElement, sourceOfTruth, opts, index)

Creates single-line address lookup with the current Address lookup APIs on the supplied input and country elements. It returns the response from find service and the callback is available via onSelect option.

For retrieve service, the response is available via ui.onRetrieveItem and the callback is available via onRetrieve option. For internationalGeocode service, if getIntlGeocode options is enabled, the response is available via ui.onIntlGeocodeItem and the callback is available via onIntlGeocode option.

element

Type: Object The input address element to create the lookup for (e.g. document.getElementById("address")).

countryElement

Type: String or Object The Country used for the lookup. It can be String (e.g. Harmony.AUSTRALIA for Australia) or Object in input or select Country element (e.g. document.getElementById("country")).

sourceOfTruth

Type: String or Object The Source of Truth to override the default Australia and New Zealand SOTs. It is optional. It can be String (e.g. Harmony.AUPAF for Australia) or Object in input or select Source of Truth element (e.g. document.getElementById("sourceOfTruth")).

opts

Type: Object Optional parameter to be passed into the auto-complete component (e.g. getIntlGeocode true/false to enable international geocode call for the selected address, default is false. And callback functions e.g. onRetrieve and onIntlGeocode in below sample).

index

Type: String Optional index to indicate which set of fields to populate if there are multiple lookups.

The following example creates a single-line address lookup (use AUPAF for Australian addresses) on the input element with ID singleLineAddress and country and customized opts:


    let opts = {
        // min 3 chars to trigger the lookup
        minLength: 3,
        //autocomplete function will only be called once within the specified time frame (ms)
        delay: 500,
        // enable Harmony.International.getGeocode for non-AU/NZ countries when address selected.
        getIntlGeocode: true,
        //Override onSelect function if applicable. It is Optional
        onSelect: function (ui) {
            console.log('address selected: ' + JSON.stringify(ui));
        },
        //Override onIntlGeocode function if applicable. It is Optional
		//e.g. get ui.onIntlGeocodeItem object for address payload after onIntlGeocode call.
        onIntlGeocode: function (ui) {
            console.log('geocode address selected: ' + JSON.stringify(ui.onIntlGeocodeItem));
        },
        //Override onRetrieve function if applicable. It is Optional
		//e.g. get ui.onRetrieveItem object for address payload after onRetrieve call.
        onRetrieve: function (ui) {
            console.log('onRetrieve address selected: ' + JSON.stringify(ui.onRetrieveItem));
        }
    };

    let input = document.getElementById("addressField");
    let country = document.getElementById("countryField");
    Harmony.useFeatureOptions({ "exposeAttributes": 0, "groupAddresses": 1 });
    HarmonyJS.addressLookup(input, country, null, opts);

Code snippet

The id of your html input element

After generating, copy and paste the following code snippet into your web page, ideally just before the </html> tag