Ricardo Alves
Ricardo Alves

Reputation: 561

jQuery geocomplete street_address not working

I have been knocking my head to the walls today trying to get the field result from jQuery Geocomplete to display street_address but it looks the plugin is not rendering it as a data-geo="" onto my form field or anything else!.

Bellow on my HTML i have the fields to pick up the street name and another for number i need the result from both to go to #BillingAddress. I believe some JavaScript may do the work but I am not an expert on it.

<div class="item">
<input id="autocomplete" placeholder="Look up your address" type="text"></input>
</div>
<div class="item" style="display: none;">
<input class="cat_textbox" id="houseNo" data-geo="street_number" type="text" placeholder="House Number" maxlength="50"/>
</div>
<div class="item" style="display: none;">
<input class="cat_textbox" id="street" data-geo="route" type="text" placeholder="street" maxlength="50"/>
</div>
<div class="item">
<input class="cat_textbox" id="BillingAddress" data-geo="street_address" type="text" placeholder="Billing Address" maxlength="50" name="BillingAddress"/>
</div>

So far I have tried using some jquery to trnasfer the fields values to the #BillingAddress input but it only copies if the other inputs are keyed or pressed, clicked, but I want them to keep hidden to improve visibility and make the form less complex for some people, so when the Geo results is chosen they populate into this field together.

$('#houseNo').on('propertychange change click keyup input paste',function(){
   $('#BillingAddress').val(this.value+' '+$('#street').val());
});

$('#street').on('propertychange change click keyup input paste', function(){
   $('#BillingAddress').val($('#houseNo').val()+' '+this.value);
});

Much appreciated the help and i think this is also a good query for some others out there.

HERE IS THE FIDDLE

Upvotes: 3

Views: 1765

Answers (2)

Archonic
Archonic

Reputation: 5362

As you probably already know, modifying the plugin isn't a great thing to do. There's a simpler way to join elements of the results object. Just bind to the result.

$('#geocomplete').geocomplete({
  details: '#geo_details',
  detailsAttribute: 'data-geo',
  types: ['geocode', 'establishment']
}).bind("geocode:result", function(e, r) {
  return $('[data-geo=street_address]').val(r['address_components'][0]['short_name'] + ' ' + r['address_components'][1]['short_name']);
});

Upvotes: 1

Ricardo Alves
Ricardo Alves

Reputation: 561

fillDetails: function(result){ "result" object doesn't have "street_address" in "address_components", it is set as a separate property of result - "name". If you just typed in search term and submitted, "name" is being returned. If you click "find" again, "name" is not being returned.

I made a quick "fix" to replace "street_address", on line 338 I added following:

    street_addr: result.formatted_address.split(',')[0],

So lines 335-339 look like this:

  // Add infos about the address and geometry.
  $.extend(data, {
    formatted_address: result.formatted_address,
    street_addr: result.formatted_address.split(',')[0],
    location_type: geometry.location_type || "PLACES",

And added "street_addr" on line 65:

"formatted_address street_addr location_type bounds").split(" ");

Upvotes: 2

Related Questions