getAddress.io API脚本

时间:2019-07-01 11:31:18

标签: javascript jquery

我正在尝试将line_1 line_2和line_3的输出全部放在称为“地址”的同一字段中,而不是将它们全部放在自己的输入字段中。

我正在使用getAddress.io中的API,下面使用的代码是我所能做到的,并竭尽全力,需要专业人员的帮助:)

目前使用该代码显示的地址如下:

第1行:平14 第2行:林登苑 3号线:​​大街

我想做的是在同一行上获得输出,如下所示:

地址:Main Street林登法院14室

$('#postcode_lookup').getAddress({
  api_key: 'MYAPIKEY',
  output_fields: {
    line_1: '#address',
    line_2: '#line2',
    line_3: '#line3',
    post_town: '#town',
    county: '#county',
    postcode: '#postcode'
  },
  onAddressSelected: function(elem, index) {
    let postcode = $('#getaddress_input').val();
    $('#postcode').val(postcode.toUpperCase());
  }
});

2 个答案:

答案 0 :(得分:1)

您只需要将elem传递到div中即可:

onAddressSelected: function(elem,index){
    $('#postcode').val(elem);
}

请在这里https://jsfiddle.net/semh9bv3/

您可以用逗号分隔地址,但我不知道它的准确性:

let address = elem.split(',');
let line1 = address[0];
let line2 = address[1];
let line3 = address[2];
let town = address[3];
let county = address[4];
let postcode = address[5];
$('#postcode').val(line1 + line2 + line3);

查看更新的小提琴https://jsfiddle.net/1gpxuzL2/

对于您的代码,它与我的更新类似:

$('#postcode_lookup').getAddress({
  api_key: 'cHCcb9duOU6-o9q1F3-9JQ19989',
  output_fields: {
    line_1: '#address',
    line_2: '#line2',
    line_3: '#line3',
    post_town: '#town',
    county: '#county',
    postcode: '#postcode'
  },
  onAddressSelected: function(elem, index) {
    let address = elem.split(',');
    let line1 = address[0];
    let line2 = address[1];
    let line3 = address[2];
    let town = address[3];
    let county = address[4];
    let postcode = address[5];
    $('#postcode').val(line1 + line2 + line3);
  }
});

在此处https://jsfiddle.net/50kfnyo8/

查看更新的小提琴以及您的代码。

答案 1 :(得分:0)

您可以检查JS的源代码以探索更多选项,但是某些文档还是可以的。

f = {
        api_key: "",
        output_fields: {
            line_1: "#line1",
            line_2: "#line2",
            line_3: "#line3",
            post_town: "#town",
            county: "#county",
            postcode: "#postcode"
        },
        api_endpoint: u,
        input: undefined,
        $input: undefined,
        input_label: "Enter your Postcode",
        input_muted_style: "color:#CBCBCB;",
        input_class: "",
        input_id: "getaddress_input",
        input_name: "getaddress_input",
        button: undefined,
        $button: undefined,
        button_id: "getaddress_button",
        button_label: "Find your Address",
        button_class: "",
        button_disabled_message: "Fetching Addresses...",
        $dropdown: undefined,
        dropdown_id: "getaddress_dropdown",
        dropdown_select_message: "Select your Address",
        dropdown_class: "",
        $error_message: undefined,
        error_message_id: "getaddress_error_message",
        error_message_postcode_invalid: "Please recheck your postcode, it seems to be incorrect",
        error_message_postcode_not_found: "Your postcode could not be found. Please type in your address",
        error_message_default: "We were not able to your address from your Postcode. Please input your address manually",
        error_message_class: "",
        lookup_interval: 1e3,
        debug_mode: !1,
        onLookupSuccess: undefined,
        onLookupError: undefined,
        onAddressSelected: undefined,
        sort_numerically: !0
    };