1

I'm trying to use the jquery library geocomplete to autocomplete a google address, and write to some hidden fields variables like the long, lat, location_type. It works for lat,lng which get written out, but I'm pulling my hairs to get the location type. Here's what I've tried:

#theJS

$(function(){
    $("#id_address").geocomplete()
      .bind("geocode:result", function(event, result){
        $.log(result.formatted_address);
          $("input#id_lat").val(result.geometry.location.lat());
          $("input#id_lng").val(result.geometry.location.lng());
          $("input#id_location_type").val(result.geometry.location_type());
      })
    });

#the HTML forms

<input id="id_lng" name="lng" type="hidden" />
<input id="id_lat" name="lat" type="hidden" />
<input id="id_location_type" name="location_type" type="text" />
<input type="text" name="address" id="id_address" />

To troubleshoot the results, I tried $("input#id_location_type").val(result.geometry); and it does write to the location_type input box [object Object], but as soon as I extend it to result.geometry.location_type or result.geometry.location_type() I get nothing.

Any tips appreciated. (here's a jsfiddle to clarify what I'm trying to to)

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Massagran
  • 1,781
  • 1
  • 20
  • 29

2 Answers2

0

Try

$("input#id_location_type").val(result.types);

instead of

$("input#id_location_type").val(result.geometry.location_type());
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
mnik
  • 41
  • 2
0

Basically, You have to bind your geocode method with geocode:result and then you can extract lat and lng.

$("#location")
  .geocomplete()
  .bind("geocode:result", function (event, result) {
  $("#latitude").val(result.geometry.location.lat());
  $("#longitude").val(result.geometry.location.lng());
  //console.log(result);
});

You can read further here

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Hamza Khan
  • 1,321
  • 11
  • 15