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!

Dani AI

Generated

For the scenario described by (≈5,000 rows) there are three practical paths: 1) use Google’s Geocoding API for per-address requests and backfill via a queued worker/cron (no single “upload CSV” endpoint on Google), 2) use a dedicated bulk geocoding provider that accepts spreadsheets or batch jobs, or 3) self-host an OpenStreetMap/Nominatim instance for full control. The queued cron / worker pattern that suggested is the right architecture if sticking with per-request APIs; for a one-off import a bulk provider (spreadsheet upload) is often faster and simpler. Geocodio (spreadsheet upload) and Mapbox’s batch/permanent geocoding options are examples. (geocod.io)

Important operational notes about Google’s current model: the Geocoding service now requires an API key with billing enabled and is billed per request (the old free 2,500/day assumption no longer applies). Rate limits are enforced (the service is measured in queries-per-minute), so backfills should be throttled and possibly spread over multiple runs to control cost and avoid errors. Plan for throttling, logging, and retries in any automated worker. [Geocoding usage & quota guidance]. (developers.google.com)

Licensing and storage constraints matter. Google’s Terms disallow mass downloading and limit caching/storage of Maps content (temporary caches only—commonly up to 30 days); offering a batch geocode feed built on Google data is not permitted. The safe, durable identifier to store from Google is the Place ID (Place IDs are exempt from the short-cache restriction but should be refreshed periodically). Review the Maps Platform Terms and Place ID guidance before deciding to persist results. (developers.google.com)

Suggested implementation checklist (no duplicate code here): normalize addresses first; insert new records into a queue table with status fields; run a CLI worker (avoid web-request timeouts) that pulls N rows, throttles to the provider’s QPS/QPM, applies exponential backoff on 429/5xx, logs provider response and match quality, and stores lat/lng + formatted address + provider place_id or id. For a single 5k backfill, evaluate paid bulk providers (cost, allowed storage, match quality) before burning per-request quota.

Recommended Answers

All 4 Replies

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?

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.