Google Maps API Version 3 Geocoding Example

Today I realized that my location markers were not being displayed in all the google maps implementations I had put into place online, and after digging into it (trying to figure out what a code 610 response meant) I realized that I was using version 2 of the google maps api for retrieving latitude and longitude based on a given address (ie “geocoding”).

My actual maps were being displayed using version 3 of the API but the example I found a couple years ago and followed for geocoding used version 2, which was deprecated and will no longer be supported sometime in 2013 (I’ve heard both March and September, but since my code isn’t working in April I suppose the March date was more accurate).

Since I wasn’t able to find an example for just what I needed (getting the latitude and longitude based on an address, so I could show markers on a google map) I read through the docs and updated my code.  See below for my implementation (I use this as an included file wherever I need to pull latitude/longitude, it could be put into a function if you want). The address, city, state, and zip should be stored in the $address, $city, $state, and $zip params, and I am also saving the latitude and longitude in both a local database and a markers array to be used in a google map later:

/*
 * File: incGetGeoCodes.php
 * Description: this file pulls the lat/long coordinates using Version 3 of the google maps api
 * 	The key parameter caused it to fail so I removed that and it works, but we may be under a 
 * 	lower daily usage limit because of that
 */
// Initialize delay in geocode speed
$delay = 0;
$base_url="http://maps.googleapis.com/maps/api/geocode/json?sensor=false";

$geoaddress = $address . ", " . $city . ", " . $state . " " . $zip;
$request_url = $base_url . "&address=" . urlencode($geoaddress);
$json=file_get_contents($request_url);
$resultArr=json_decode($json,true);

$error='';
$status = $resultArr['status'];
if (strcmp($status, "OK") == 0) {
	// Successful geocode
	$markers[$shop_id]['lat'] = $resultArr['results'][0]['geometry']['location']['lat'];
	$markers[$shop_id]['long'] = $resultArr['results'][0]['geometry']['location']['lng'];
	$markers[$shop_id]['html'] = $name."<br>".$address."<br>".$city." ".$state." ".$zip."<br>".$phone;
	
	#Now update the db so we don't have to pull this again
	$query = "update entities ".
		"set shop_latitude=".addslashes($markers[$shop_id]['lat']).", ".
		"shop_longitude=".addslashes($markers[$shop_id]['long'])." ".
		"where EntityID=".intval($EntityID);
	mysql_query($query); $err=mysql_error(); if (!empty($err)) echo "query:$query, error: ".$err."<br>";
} else if (strcmp($status, "620") == 0) {
	// sent geocodes too fast
	$delay += 100000;
} else {
	// failure to geocode
	$error .= urlencode("Address " . $geoaddress . " failed to be geocoded. ");
	$error .= urlencode("Received status " . $status . "%0D%0A");
}
usleep($delay);

I was surprised to see no mention of an API key parameter to be used, if someone knows if that is an option (to increase the daily quota of geocoding api calls that can be made) please leave a comment and let me know. I’m just happy to get it working again for now. 🙂

Leave a comment

Your email address will not be published. Required fields are marked *

CommentLuv badge