naeemqaswar
12/28/2018 - 11:00 AM

Autocomplete Custom Response

jQuery("#address").autocomplete({

  //This bit uses the geocoder to fetch address values
  source: function(request, response) {

    geocoder.geocode( {'address': request.term }, function(results, status) {

      response(jQuery.map(results, function(item) {

        return {

          label:  item.formatted_address,

          value: item.formatted_address,

          latitude: item.geometry.location.lat(),

          longitude: item.geometry.location.lng()

        }

      }));
    })
  },

  //This bit is executed upon selection of an address
  select: function(event, ui) {

    jQuery("#latitude").val(ui.item.latitude);

    jQuery("#longitude").val(ui.item.longitude);

    var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);

    marker.setPosition(location);

    map.setZoom(16);

    map.setCenter(location);
  }
});