PHP server side geocoding with Google Maps API v3

All of us know this very useful service of Google Maps. And lot of programmers already triev Google Maps API v3. Somebody even met the problem with the limit of the geo coding requests per time period. In some case server-side scrip can help to solve the problem. If the number of the locations is less than 12.000 (monthly limit of requests per IP), you can do geocoding server side and save the results to the database. If your application requires more geocoding requests than you should implement some sort of client-side geo coding strategy. This will use IPs of your site visitors.

As usual I have prepared some code. Right now that is php class that implements server-side strategy of geocoding as a wrapper for the http requests to Google Maps service. So here is  the wrapper class:

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

	static public function getLocation($address){
		$url = self::$url.urlencode($address);
		
		$resp_json = self::curl_file_get_contents($url);
		$resp = json_decode($resp_json, true);

		if($resp['status']='OK'){
			return $resp['results'][0]['geometry']['location'];
		}else{
			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);
		curl_close($c);

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

Here I should explain why I am using curl to make the request. Not all of the hosts are capable to use file_get_contents to fetch content from the remote host, but curl is available almost everywhere. If you do not have it on you server just replace the line

$resp_json = self::curl_file_get_contents($url);

to

$resp_json = file_get_contents($url);

So, here is a brief example of how to use the wrapper class:

$address=urlencode("1600 Amphitheatre Parkway, Mountain View, CA");
$loc = geocoder::getLocation($address);

print_r($loc);

As I have said before you can save the location to the database, when you will need to show it on the map, just use the following Javascript code:

var chicago = new google.maps.LatLng(<?=$Lat;?>, <?=$Lng;?>);

Sure this point can be used to create marker:

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  function initialize() {
    var myLatlng = new google.maps.LatLng(<?=$Lat;?>, <?=$Lng;?>);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });   
  }
</script>

I hope this article was useful.

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)