views:

49

answers:

3

I'm currently creating an estate agents website with a search which returns a set of properties stored in the database.

Now I know that google has a limit on how many requests it has per day so I'm trying to think of the best way to do this process.

Basically at the moment, step one of the search is to input a town or postcode with which I was going to send a request to the google maps api to get the latitude and longitude of these and store them in the database for future use if they weren't already in there.

Do you think this would be ok? Or is there a better solution around this?

Thanks.

A: 

Just run the code from the user browser -- the API usage count will be count against the user's IP address.

J-16 SDiZ
Are you sure this counts against IP address? I seem to remember we ran into this limit *against API key* - our initial implementation was quite naive and each user's browser made some 10 requests from their own IP. We started hitting the `OVER_QUERY_LIMIT` in a few hours (we solved that by caching the results for a short time, most people were requesting the same objects). This was some time ago in v2 and the documentation is vague here, see http://code.google.com/apis/maps/documentation/geocoding/#Limits
Piskvor
Hi,Well I currently have this:<?php$code = 'NE1 1RJ';$mapsApiKey = 'your-google-maps-api-key';$query = "http://maps.google.co.uk/maps/geo?q=".urlencode($code)."$data = file_get_contents($query);if($data){ // convert into readable format $data = json_decode($data); $long = $data->Placemark[0]->Point->coordinates[0]; $lat = $data->Placemark[0]->Point->coordinates[1]; echo $long; echo $lat;}else { return false;}?>so will that be ok? Thanks.
Ashley
@Piskvor: the faq say so : http://code.google.com/intl/en/apis/maps/faq.html
J-16 SDiZ
@Ashley: no. file_get_contents() fetch from your server, this is count as your server ip. you have to run it from javascript, not php. If you must, run it from javascript, and resubmit it to your server.
J-16 SDiZ
@J-16 SDiZ: aha, here it is: http://code.google.com/intl/en/apis/maps/faq.html#geocoder_limit Thanks for pointing it out, didn't notice that.
Piskvor
+2  A: 

Hi,

Have a look at this site. It worked perfect for me: http://www.geonames.org/export/ws-overview.html

I only used the function "findNearbyStreets" (http://www.geonames.org/maps/us-reverse-geocoder.html#findNearbyStreets), and this was my code:

<?php
$sFile = "list.csv";
$sContent = file_get_contents($sFile);

$aRows  = explode("\n", $sContent);
$sEcho = "";
$i      = 1;

echo "Total rows: " . count($aRows) . "\n===============\n\n";

foreach ($aRows as $sRow)
{
    $aCols  = explode(",", $sRow);

    $sLongitude = $aCols[0];
    $sLatitude = $aCols[1];
    $sInfo = ucfirst($aCols[2]);
    $sInfo = str_replace('"', '', $sInfo);

     $url = "http://ws.geonames.org/findNearbyStreetsOSMJSON?lat=$sLatitude&amp;lng=$sLongitude";
     $json = file_get_contents($url);
     $data = json_decode($json, true);

     $street = $data['streetSegment'][0]['name'];

    $sEcho .= "Some text";

    echo "Done with row: $i/".count($aRows)." \n";
    $i++;
}

$rFile = fopen("newFile.txt", "w+");
fwrite($rFile, $sEcho);
fclose($rFile);
?>

Regards, Paul

Paul Peelen
Just to clarify: The script I wrote was written for shell usage, but can just as well be used for webusage (just remove the echo's ;) )
Paul Peelen
+1  A: 

Your approach looks OK - the address of a real estate propety won't change too often, I assume ;)

You could even store the per-property coordinates, e.g. "123 Foo Street, Barb AZ is 35.7, -111.0" and only look them up in geocoder if you don't already have them in database.

Piskvor
Yes that's what I was going to do, so overtime there'll be less and less requests to google, but I'm still concerned over the initial launch...
Ashley
Well, if you have the data before the official launch, you could seed the database with them - look up the most likely cities and in case you later run over the limit, offer just the city location.
Piskvor