Loading...

JavaScript - Location

Add as-you-type location completion to any HTML form. Try it

Example

Install from NPM or CDN

npm install getaddress-location
<script src="https://cdn.getaddress.io/scripts/getaddress-location-1.0.0.min.js"></script>

Usage

<input id="textbox_id" type="text"> 
<script> getAddress.location( 'textbox_id', 'API Key or Domain Token' ); </script>

Events

document.addEventListener("getaddress-location-suggestions", function(e){
    console.log(e.suggestions);
})

document.addEventListener("getaddress-location-suggestions-failed", function(e){
    console.log(e.status);
    console.log(e.message);
})

document.addEventListener("getaddress-location-selected", function(e){
    console.log(e.location);
})

document.addEventListener("getaddress-location-selected-failed", function(e){
    console.log(e.status);
    console.log(e.message);
})

Options

getAddress.location(
    'textbox_id',
    'API Key',
    /*options*/{
        output_fields:{
            latitude:'latitude',  /* The id of the element bound to 'latitude' */
            longitude:'longitude',  /* The id of the element bound to 'longitude' */
            town_or_city:'town_or_city',  /* The id of the element bound to 'town_or_city' */
            area:'area',  /* The id of the element bound to 'area' */
            county:'county',  /* The id of the element bound to 'county' */
            country:'country',  /* The id of the element bound to 'country' */
            postcode:'postcode',  /* The id of the element bound to 'postcode' */
            outcode:'outcode'  /* The id of the element bound to 'outcode' */
        },
        id_prefix:'getAddress-location' ,  /* The id of the textbox and list container */
        css_prefix?:'getAddress_location'",  /* The class name prefix */
        delay:200, /* millisecond delay between keypress and API call */
        minimum_characters:2,  /* minimum characters to initiate an API call */
        clear_list_on_select:true, /* if true, clears list on suggestion selected */
        select_on_focus:true,  /* if true, highlights textbox characters on focus*/
        alt_location_url:undefined,  /* alterative local autocomplete URL (when API key is not used) */
        alt_get_location_url:undefined,  /* alterative local get URL (when API key is not used) */
        input_class_names:[],  /* textbox class names */
        input_show_class_names:[],  /* textbox class names on show */
        list_class_names:[],  /* list class names */
        container_class_names:[], /* container class names */
        suggestion_class_names:[], /* suggestion class names */
        highlight_suggestion:true, /* if true, highlights matched suggestion text */
        highlight_suggestion_start_tag:'<b>',  /* highlighted suggestion text start tag */
        highlight_suggestion_end_tag:'</b>',  /* highlighted suggestion text end tag */
        list_width:undefined,   /* if true, set the list width */
        suggestion_count:6, /* number of retreived suggestions (max 20) */
        auto_calc_list_height:true,   /* if true, calculates the list's height */
        suggestion_template:undefined, /* the suggestion template (see Autocomplete API)*/
        filter:undefined, /* the suggestion filter (see Autocomplete API)*/
        bind_output_fields:true, /* if true, bind the output_fields to the address*/
        input_focus_on_select:true,  /* if true, sets the focus to the textbox after selecting an address*/
        debug:false, /* if true, logs behavior */
        enable_get_location:true , /* if true, retreives address on select */
        clear_on_select:false /* if true, clears textbox after selecting a location */
    }
);

Domain Tokens

To avoid exposing your API key in browser code, Domain Tokens can used in place of your API key.
Domain Tokens can be generated for a specific domain and it's sub-domains. They can also limit the number of look-ups per minute for an IP address.

<script>
    getAddress.location
    (
        'textbox_id',
        'dtoken_hEDzcyiWMI1eXXXX'
    );
</script>
Top