1

I am trying to geocode an array of postcodes from a json file using gmaps.js.

There are 101 postcodes in the array however when I loop through them to use with gmaps.js it only iterates through 11. I have tried looking around for answers, however i can't figure it out, everything looks correct, here is my code

var getPostcodes = function () {
                    var json = "http://example.co.uk/addresses.json";

                    var request = new XMLHttpRequest();
                    request.open('GET', json, true);

                    request.onload = function () {
                        if (request.status >= 200 && request.status < 400) {
                            // Success!
                            var data = JSON.parse(request.responseText);

                            //GET POSTCODES IN ARRAY
                            var postcodesArray = [];
                            for (var i = 0; i < data.length; i++) {
                                postcodesArray.push(data[i].from_postcode);
                            }
                            var postcodes = postcodesArray.filter(Boolean);

                            //GET LAT/LONG POSITIONS
                            for (var n = 0; n < postcodes.length; n++) {
                                GMaps.geocode({
                                    address: postcodes[n],
                                    callback: function (results, status) {
                                        if (status == 'OK') {
                                            var location = results[0].geometry.location;

                                            var latitude = parseFloat(location.lat().toFixed(8)),
                                                longitude = parseFloat(location.lng().toFixed(8));

                                            console.log(latitude + ' ' + longitude);
                                        }
                                    }
                                });
                            }
                        }
                        else {
                            console.log("failed");
                        }
                    };
                    request.send();
                }

                getPostcodes();

//EDIT console log

enter image description here

//EDIT codepen link

http://codepen.io/anon/pen/gwWgzB

Sai
  • 801
  • 3
  • 10
  • 27

1 Answers1

-1

Google limits the number of geocoding requests per second. I suggest you to put sleep between consecutive requests.

Deepak Sharma
  • 409
  • 4
  • 14
  • `put sleep between consecutive requests` - sleep? – Jaromanda X Sep 26 '16 at 14:02
  • Is there a limit to how many you can geocode? for some reason it's always after 11 postcodes it stops – Sai Sep 26 '16 at 15:14
  • Yes. its 50 request per second and 2500 request per day. But if you don't use api key then it will stop after 11. Sleep between consecutive request means do not iterate all of the postcodes at once. – Deepak Sharma Sep 27 '16 at 05:35