Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Geocoding Addresses

Is there a way using php to Geocode a lot of addresses (about 4500) in a MySQL database using php and V2 or V3 of the Google Maps API? I have a version that supposedly worked in V1 of the API, but now it is producing the wrong result (it ignores the street address & just uses the city) for about 50% of the addresses.

I attach the code I have now.
geocode-custs.php
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a lot of addresses (about 4500)...
Afterthought... The geocoders may be rate-limited and you might want to sleep(1) between calls to avoid overloading the servers and getting your IP address banned.  You might also want to check the return values to compare Google's answer to Yahoo's answer.  In almost all cases they will be very close, but in large-scale geocoding exercises using thousands of addresses, I have found that there are occasionally discrepancies.  The way I handled this was to store both Google and Yahoo lat/lon pairs in my data base of addresses.  Then I wrote a little script to run through the data base and compute the distance from the Google address to the Yahoo address.  If there was a discrepancy of more than a half mile I flagged the row for manual intervention.

Overall I found that Google was more "creative" when it came to addresses that it did not understand, occasionally giving me locations in the ocean or Eastern Europe.  Yahoo was not as creative, returning only an indicator that it did not understand the address.  Google got more addresses right than Yahoo.  I didn't keep statistics, but I seem to remember that if Google got 98% correct, Yahoo probably got something like 95% correct.
Avatar of Richard Korts

ASKER

A couple of points.

(1) The line: $xml = simplexml_load_string($this->getCurlData($url)) or die(""); produces a php error. Removing the '$this->' fixes it.

(2) If you try to do a lot of them quickly, it starts returning spaces for lat / long. I used your functions but did it like this:

            $nf = true;
            while ($nf) {            
                        $latlon = getLatLon($addr, $cs, "USA");
                        if ($latlon[0] == "") {
                               sleep(1);
                        } else {
                               $nf = false;
                        }
            }

With those changes, I geocoded about 4500 addresses in less than an hour.
Hi, Thankks for point. I apologize for the problems you found: I toke these two function from a my class and I forgot a $this-> :-)

Since sleep(1) has been suggestewd by Ray, you can consider to change your decision and split points between me and Ray. You can do it requesting attention to the moderator.

Good luck with your project.

Marco
Points are not a problem - I have enough points to orbit Saturn.  Best to all, ~Ray
To marqusG,

I agree. How do I request attention on a closed question?
Clicking the Request attention link below the question box at the top of the page.

Cheers