Update for Geocoding Google maps using Maps API V3

I’ve recently updated the geocoding system posted previously (many years ago) to work better with the latest google maps API (V3).  The previous post worked fine until fairly recently then something changes.  This new method is more OO based and relies on a class for geocoding which you can call at any point you like.

Much easier to use, less code, more reuseable and work 100% with latest google API 🙂


class geocoder{
        static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";

        static public function getLocation($address){
            $url = self::$url.$address;

            $resp_json = self::curl_file_get_contents($url);
            $resp = json_decode($resp_json, true);

                return $resp['results'][0]['geometry']['location'];
                return false;

        static private function curl_file_get_contents($URL){
            $c = curl_init();
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($c, CURLOPT_URL, $URL);
            $contents = curl_exec($c);

            if ($contents) return $contents;
                else return FALSE;


Now to use it

include "system/geocode.class.php";
$address = urlencode(trim($incoming_address));
$loc = geocoder::getLocation($address);
echo "Lat: ".$loc->["lat"];
echo "<br />";
echo "Long: ".$loc->["lng"];

Published by


FC Software Technical Director