Google Maps Autocomplete List

IRHM picture IRHM · May 28, 2012 · Viewed 9.5k times · Source

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

Answer

Gaurav Agrawal picture Gaurav Agrawal · May 28, 2012

The Google Maps V3 API now incudes an autocomplete feature that will adapt any text input field to autocomplete locations &/or Places. It doesn't show thumbnails yet but the addition of Places will be great for some use cases, here's the details:

Documentation: http://code.google.com/apis/maps/documentation/javascript/places.html#places_autocomplete

Reference: http://code.google.com/apis/maps/documentation/javascript/reference.html#Autocomplete

Example: http://code.google.com/apis/maps/documentation/javascript/examples/places-autocomplete.html

I think these will help you.....