Search code examples
javascriptgoogle-mapsautocompletegoogle-apigoogle-maps-autocomplete

Google Maps Autocomplete List


I wonder whether someone may be able to help me please.

I'm using the code below to perform an Google Maps Autocomplete action upon a user entering address details:

// JavaScript Document
var geocoder;
var map;
var marker;

function initialize(){
  var myOptions = {
    zoom: 6,
    center: new google.maps.LatLng(54.312195845815246, -4.45948481875007),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    mapTypeControl: true,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
      position: google.maps.ControlPosition.TOP_RIGHT
    },
    navigationControl: true,
    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.ZOOM_PAN,
      position: google.maps.ControlPosition.TOP_LEFT
    },
    scaleControl: true,
    scaleControlOptions: {
      position: google.maps.ControlPosition.BOTTOM_LEFT
    }
  };

  map = new google.maps.Map(document.getElementById('map'), myOptions);
  geocoder = new google.maps.Geocoder();
  marker = new google.maps.Marker({
    map: map,
    draggable: true
  });
}

$(document).ready(function() { 
  initialize();
  $(function() {
    $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source: function(request, response) {       
        geocoder.geocode( {'address': request.term }, function(results, status) {
          response($.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) {
        $("#osgb36lat").val(ui.item.latitude);
        $("#osgb36lon").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);

      }
    });
  });

  google.maps.event.addListener(marker, 'dragend', function() {
    geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
          $('#address').val(results[0].formatted_address);
          $('#osgb36lat').val(marker.getPosition().lat());
          $('#osgb36lon').val(marker.getPosition().lng());

          var point = marker.getPosition();
          map.panTo(point);
        }
      }
    });
  });
})

and this is how I place it within my form.

<div>
  <input name="address" type="text" id="address" size="40" maxlength="40" style="font:Calibri; font-size:12px" />
</div>

What I'd now like to be able to do is limit the number of items viewable in the list. I've done some research on several forums and on the Google Maps site but I can't seem to find a solution, so I'm not even sure whether this is possible.

I just wondered whether someone could possibly have a look at this please and provide a little guidance on how I may be able to go about this.

Many thanks and kind regards


Solution

  • After some further research, I found a great tutorial here: http://rjshade.com/2012/03/27/Google-Maps-autocomplete-with-jQuery-UI/ which I've been able to adapt.