PHP Google Geocode an Address Example

How to Google Geocode an Address with PHP

PHP Google Geocode an Address ExampleI have a properly listing site I support that allows users to search for properties within a distance from a specific zip code. Providing a search by zip code is very simple. Providing a search for a given distance from a specific zip code is also pretty simple if you have the geographic coordinates of the target zip code. This is where Google’s Geocoding service comes in handy.


To obtain the geographic coordinates for a specific address or zip code you need only call Googles Geocode service and provide the address to geocode. Below is an example function I use to get the geographic coordinates of an address. I use this function to geocode each property listing that is saved to the database. Every time the property is saved in the database I also update the geographic coordinates, in case the property owner had to correct the address. The database table has columns for latitude, longitude, and formatted address.

Below the function is an example calling the function to geocode an address by zip code only.

function fnGeocode($address)
{
	$address = urlencode($address);											// url encode the address - can be an address or just a zip
	$url = "https://maps.google.com/maps/api/geocode/json?address={$address}";					// google map geocode api url
	$resp_json = file_get_contents($url);										// get the json response
	$resp = json_decode($resp_json, true);										// decode the json
	if($resp['status'] == 'OK')											// response status will be 'OK', if able to geocode given address 
	{
	        // get the geocode results
		$lat = $resp['results'][0]['geometry']['location']['lat'];
		$lon = $resp['results'][0]['geometry']['location']['lng'];
		$formatted_address = $resp['results'][0]['formatted_address'];
       
		if ($lat && $lon && $formatted_address)									// is complete
		{
			$coodinates = array($lat, $lon, $formatted_address);						// put the results in an array
			return $coodinates;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// example usage

	$listing_id = 12345;
	$listing_zip = 90210;
	$coodinates = fnGeocode($listing_zip);
	if ($coodinates !== false)
	{
		$listing_geolatitude = $coodinates[0];
		$listing_geolongitude = $coodinates[1];
		$listing_geolocation = $coodinates[2];
		$query = "UPDATE tbl_listings";
		$query .= " SET listing_geolatitude = '$listing_geolatitude'";
		$query .= " , listing_geolongitude = '$listing_geolongitude'";
		$query .= " , listing_geolocation = '$listing_geolocation'";
		$query .= " WHERE listing_id = $listing_id";
		queryUpdate($query);											// external function to process the query
	}


Copy Code

References

PHP urlencode
PHP file_get_contents
PHP json_decode

Leave a Reply

I appreciate and welcome your comments. Please keep in mind that comments are moderated according to my comment policy.
Your email address will not be published. Required fields are marked *