0

I modified the javascript from https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple to

var geocoder;
var postalArr = []; 
postalArr.push(249586);
postalArr.push(266751);
var map;

function initialize(){

 var myLatlng = new google.maps.LatLng(1.3667, 103.7500);
   var myOptions = {
     zoom: 13,
     center: myLatlng,
     mapTypeId: google.maps.MapTypeId.ROADMAP,
   };

  if (postalArr) {
      for (var i = 0; i < postalArr.length; i++ ) {
        codeAddress(postalArr[i]);
     }
  }

  map = new google.maps.Map(document.getElementById('map_canvas'),
      myOptions);  
}

function codeAddress(postal) {
    geocoder.geocode( { 'postal': postal}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var markerE = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

The script goes within the for loop but doesn't run the codeAddress function. I'm not sure why.

consprice
  • 722
  • 3
  • 11
  • 21

1 Answers1

0

Two things.

(1) need to define geocoder somewhere, I put it in the initialize

function initialize(){
  geocoder = new google.maps.Geocoder();

(2) there's no such thing as a postal property to feed the geocoder. Valid requests are for a latlng or an address as explained here.

So at least you must specify a country. I'm not sure what country 249586 is for, in my demo I used two California zip codes, and added ", United States" to the address.

geocoder.geocode( { 'address': postal + ", United States"}, 
Tina CG Hoehr
  • 6,721
  • 6
  • 44
  • 57
  • Hmm, I still don't see the pin. 249586 is the postal code for Singapore, so I did `geocoder.geocode( { 'address': postal + ", Singapore"}` but google's example was able to locate address based on the 6 numbers so I'm not sure whether that was the issue. Thanks for pointing out the initialization of geocoder though:) – consprice Jul 10 '12 at 03:12
  • Did you change the numbers back to Singaporean postal codes? Try [**this update**](http://jsfiddle.net/yV6xv/148/) – Tina CG Hoehr Jul 10 '12 at 03:14