0

Is there a way of sending multiple addresses to through one geocode request:

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        addMarker(results[0].geometry.location); 
        var the_string = generate_string(elements, a, address);

        infowindow[a] = new google.maps.InfoWindow({
            content: the_string
        });     

        google.maps.event.addListener(markersArray[a], "click", function() {
            infowindow[a].open(map, markersArray[a]);
        });
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});       

where address contains an array of addresses. Reason is I am coming across "OVER_QUERY_LIMIT" addressed here OVER_QUERY_LIMIT while using google maps and here Google Map Javascript API v3 - OVER_QUERY_LIMIT Error

I was wondering if i should start using the server side version of the client or continue with the javascript api and get help with sending an array of addresses through the geocoder call?

In your opinion or experience, if i have an application that you can type endless amounts of addresses into fields and then press a "plot to map" button, how would you go about doing this in terms of api calls, geocoding etc?

EDIT:////////////////////////

I'm now using this code:

class geocoder{
    static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";

    static public function getLocation($address){
        $url = self::$url.urlencode($address);

        $resp_json = self::curl_file_get_contents($url);
        $resp = json_decode($resp_json, true);

        if($resp['status']='OK'){
            return $resp['results'][0]['geometry']['location'];
        }else{
            return false;
        }
    }

    static private function curl_file_get_contents($URL){
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
}      

and calling it like so:

$address = urlencode($field_set["value"]);
$loc = geocoder::getLocation($address);

in a loop of every address being placed in (in this case 20 times).

It only places 10 markers on the map regardless of the loop correctly having 2 different lngs and lats.

It returns null for the last 10 lat lngs :s

EDIT:///////////////////////////

I have placed a sleep in the loop that calls this:

$loc = geocoder::getLocation($address);

every 8th geocoding, it pauses for a second then continues.. this seems to allow all geocoder locations to be rendered correctly... but it seems like a fluff.

Any ideas?

Community
  • 1
  • 1
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • possible duplicate http://stackoverflow.com/questions/396819/geocode-multiple-addresses – Ravi Nov 19 '12 at 13:20
  • Nah its not. seen that too ;) . I'll better my question though :). I was wondering if it can be done using the javascript api rather than the server side. – Jimmyt1988 Nov 19 '12 at 13:21
  • Ok... I just thought it might give you a quick solution.. – Ravi Nov 19 '12 at 13:23

2 Answers2

1

The best way to to this with geocode, is to store the longitude and latitude value on your server, so you won't have to send a bunch geocode requests every time your script runs

Philippe Boissonneault
  • 3,949
  • 3
  • 26
  • 33
  • This. You will have to refresh the data every so often, but this will make your map faster and more accurate (because you can tweak the data to get the points in *exactly* the right place). – Andrew Leach Nov 19 '12 at 14:05
0

No, the address have to be an address as specified in the documentation.

The geocoder will return an array of results, but you can't input an array of addresses to geocode/reverse-geocode, isn't too logic anyway and people would use it to exploit the limits.

You shouldn't worry too much on the query_limit, because it's per IP/day as long as it's javascript api and no backend. So if you have multiple users using this feature the limit is set to them individually and it doesn't sum up.

If you are going over the limit by a lot because you have a local environment, then you might need to hire the google maps for business services that have up to 100k requests daily I think.

aleation
  • 4,796
  • 1
  • 21
  • 35
  • The limit seems to be 10 calls per minute (see above class code im now using) – Jimmyt1988 Nov 19 '12 at 15:42
  • actually the limit is 10 requests per second, you could include the function in a loop that makes 5 calls and wait for a second or something like that, to avoid the 10secs limit. I am completely sure that is not 10 by minute but a few per second. – aleation Nov 19 '12 at 16:40
  • That.. my friend, is very very helpful. – Jimmyt1988 Nov 19 '12 at 16:57