Jimmy
Jimmy

Reputation: 12517

Grabbing country from google geocode jquery

I've had help to get a jquery script which will geocode a typed location via googles geocoding system.

I need the short_name of country value put into the country field. For the life of me I can't get my head around how I should be integrating, and everything I try doesn't return the value. Can anyone please give some insight as to the correct way of achieving this please?

JSFIDDLE: http://jsfiddle.net/spadez/jCB4s/2/

$(function () {
  var input = $("#loc"),
      lat   = $("#lat"),
      lng   = $("#lng"),
      lastQuery  = null,
      lastResult = null, // new!
      autocomplete;

  function processLocation(callback) { // accept a callback argument
    var query = $.trim(input.val()),
        geocoder;

    // if query is empty or the same as last time...
    if( !query || query == lastQuery ) {
      callback(lastResult); // send the same result as before
      return; // and stop here
    }

    lastQuery = query; // store for next time

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ address: query }, function(results, status) {
      if( status === google.maps.GeocoderStatus.OK ) {
        lat.val(results[0].geometry.location.lat());
        lng.val(results[0].geometry.location.lng());
        lastResult = true; // success!
      } else {
        alert("Sorry - We couldn't find this location. Please try an alternative");
        lastResult = false; // failure!
      }
      callback(lastResult); // send the result back
    });
  }

  autocomplete = new google.maps.places.Autocomplete(input[0], {
    types: ["geocode"],
    componentRestrictions: {
      country: "uk"
    }
  });

  google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

  $('#searchform').on('submit', function (event) {
    var form = this;

    event.preventDefault(); // stop the submission

    processLocation(function (success) {
      if( success ) { // if the geocoding succeeded, submit the form
        form.submit()
      }
    });

  });
});

According to the API I need this:

results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
   short_name: string,
   long_name: string,
   types[]: string
 },
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
 }
}

Upvotes: 0

Views: 2733

Answers (1)

geocodezip
geocodezip

Reputation: 161384

Parse through the results looking for one with a type of "country"

Example that does that looking for a result with a type of "postal_code"

Something like this should work (not tested):

geocoder.geocode({ address: query }, function(results, status) {
  if( status === google.maps.GeocoderStatus.OK ) {
    lat.val(results[0].geometry.location.lat());
    lng.val(results[0].geometry.location.lng());
    var address = results[0].address_components;
    for (var p = address.length-1; p >= 0; p--) {
      if (address[p].types.indexOf("country") != -1) {
      // do something useful with the country...
      // document.getElementById('postcode').innerHTML= address[p].long_name;
      }
    }
    lastResult = true; // success!
  } else {
    alert("Sorry - We couldn't find this location. Please try an alternative");
    lastResult = false; // failure!
  }
  callback(lastResult); // send the result back
});

Upvotes: 1

Related Questions