4

I am trying to Show POST-CODE Information on AutoComplete Box.

For Eg: If User look into into Address: 25 Cordwallis Road,Madienhead,SL6 7DQ... It Could Retrieve Postal Code also.

function initialize() { 
 directionsDisplay = new google.maps.DirectionsRenderer();
  var heathrow = new google.maps.LatLng(51.4706001282,-0.461941003799);
  var mapOptions = {
    center: heathrow,
    zoom:14,
    mapTypeId: google.maps.MapTypeId.ROADMAP

  };

   map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
   directionsDisplay.setMap(map);

   var inputFrom =(document.getElementById('searchFrom'));
    var options = {      
       componentRestrictions: {country: "US"}
     };
  var autocompleteFrom = new google.maps.places.Autocomplete(inputFrom, options);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(autocompleteFrom, 'place_changed', function() {
   infowindow.close();
    marker.setVisible(false);
    inputFrom.className = '';
    var place = autocompleteFrom.getPlace();
    if (!place.geometry) {
      // Inform the user that the place was not found and return.
      inputFrom.className = 'notfound';
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';

    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
    calcRoute();

  });

How I have to Use this to get post Code information

var zip = address[address.length - 1].long_name = results[0].address_components;

Anybody Could you Suggest Ideas

arunsmartarrow
  • 91
  • 1
  • 10

1 Answers1

0

You cannot solve the problem 100% because not all results that you get form address search contains postal code.

The partially solution is to fix following part of your code.

 (place.address_components[0] && place.address_components[0].short_name || ''),
 (place.address_components[1] && place.address_components[1].short_name || ''),
 (place.address_components[2] && place.address_components[2].short_name || '')

As i think you assume that postal code located at one of 3 those address_components but it not always like this.

You can have more that 3 address_components. I have some addresses that postal code is located at 6(place) address_components.

In order to find the postal code or any other component you need to know the property name, it is located at

place.address_components[i].types

For posalCode his name is "postal_code" This can be done using something like this.

for(var p = 0; p < oPlace.address_components.length ; p++){
    if(oPlace.address_components[p].types.indexOf("postal_code") > -1){
        return oPlace.address_components[p].long_name
    }
}
Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53