0

I have a question regarding Google Maps integration I am planning for my site. Right now I have a list of approximately 5,000 addresses (Street Address, City, and Zip Code). I would like to add two columns to the table these address are stored in for Longitude & Latitude so that when a user visits a particular location's page, a Google map can be served up with the chosen location being centered on the map with a marker.

I've tried reading the API; however, it doesn't seem to me there is a way to submit a request of the address I already have to get the Longitude & Latitude. It seems that the API only dynamically generates this info through JS or XML. Does anyone know if there's a way to submit something like an XLS or CSV file to get the longitude & latitude directly? Or, if there's a service that offers this?

Thanks!

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by mschroeder
0

I forgot to mention above another question - once the initial list is Geocoded, I want all locations that are added to the database, to automatically pull this value either one by one or by a daily/weekly batch file. Any idea how to do this automatically through PHP?

0

You would want to use the Geocoding API: http://code.google.com/apis/maps/documentation/geocoding/

This could be as simple as capturing the output of http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
with file_get_contents, and then using json_decode on the result to get a multi-dimensional array. If you put the url above into your browser you'll see the json response returned.

<?php
$json = file_get_content('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false');

$array = json_decode($json);

echo $array['results']['geometry']['location']['lat'].PHP_EOL;
echo $array['results']['geometry']['location']['lng'].PHP_EOL;

However if you read through the api docs, there is a 2500 request limit per day.

I see three ways you could implement the automation of this.

First, you could have a single cron script that just pulls all locations with a limit of 2500 and then processes those every night.

Second, you could actually do the request when the user submits an address initially. The issues with the second option would be the time it takes to complete the external request as the user would need to wait for that to happen before the record would be saved.

Third, you could use a queue, where when the user adds an address, instead of doing the request directly, it is added to a queue to be picked up later, and on a timed cycle, a cron script picks up only the items in queue and processes them.

Queueing is a great way to offset these kinds of tasks like sending emails, geocoding and other background tasks and there are lots of ways to handle queues.

0

Thank you so much for the post and help mschroeder! As it relates to the API and JSON (Keep in mind that I'm not as familiar with JSON) but wouldn't this method require you to manually post the addresses to the URL? Or is there some sort of way to accomplish this automatically?

As for the second part, I was thinking more of a nightly cron job that would grab all new locations from the previous day and then run and update the DB accordingly.

0

You would need to make a GET request (retrieve the api url) for every address. There is no bulk API that I am aware of.

FYI, I looked at the json a little closer and realized I posted an array example earlier. The proper way to access the lat and long return values is: $object->results[0]->geometry->location->lat and $object->results[0]->geometry->location->lng where $object is the return from json_decode();

To do this via a cron script you would:

Retrieve new addresses from database (limit at 2500 because of api)
Loop over those results
For each address:
  Request the geocode api (file_get_contents or a variant)
  Process the json response
  Update the address record with the lat and lng

The thing to be aware of, is how you are calling your php script from cron.
If you are calling it via the command line, e.g php /path/to/your/file.php the script will have its max_execution_time set to 0 so it will run until it is completed.

If you are calling the script via something like wget or curl you will need to be conscience of the maximum execution time. http://php.net/manual/en/function.set-time-limit.php

Edited by mschroeder: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.