Geocoding using PHP and Google maps API V3

Geocoding is the process of converting an address to a lat/long set of coordinates for mapping against the google maps api (or the other way around ie: coords to address).  This used to be quite a long winded job exploding arrays and all kinds of jazz in the older Google Maps API’s however as of V3 its become incrediably simple when using PHP to quickly request and retrieve the coordinates from an input address.

I’m not going to go into massive details explaining it as its mostly commented in the code you can also find more information direct from google on their maps API page including a full list of status codes and a complete breakdown of the returned XML feed. http://code.google.com/apis/maps/documentation/geocoding/

For our request we’ll be using the PHP built in function simplexml_load_file().

$postcode = urlencode("DN4 6HP"); // post code to look up in this case status however can easily be retrieved from a database or a form post
    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
    $xml = simplexml_load_file($request_url) or die("url not loading");// XML request
    $status = $xml->status;// GET the request status as google's api can return several responses
    if ($status=="OK") {
        //request returned completed time to get lat / lang for storage
        $lat = $xml->result->geometry->location->lat;
        $long = $xml->result->geometry->location->lng;
        echo "$lat,$long";  //spit out results or you can store them in a DB if you wish
    }
    if ($status=="ZERO_RESULTS") {
        //indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
    }
    if ($status=="OVER_QUERY_LIMIT") {
        //indicates that you are over your quota of geocode requests against the google api
    }
    if ($status=="REQUEST_DENIED") {
        //indicates that your request was denied, generally because of lack of a sensor parameter.
    }
    if ($status=="INVALID_REQUEST") {
        //generally indicates that the query (address or latlng) is missing.
    }

Thats it as simple as that I’ve put in the traps for the different status returns from google however not done anything with them apart from comment them its upto you how you make your application fallback.

 

THIS ARTICLE HAS BEEN UPDATED CLICK HERE TO VIEW UPDATED GEOCODING ARTICLE

MySQL Date format and PHP

This is just a very quick blog entry to provide a little snippet of code and hopefully help some people.

I offer coding help and advice on several PHP forms for beginners to the language.  One of the common problems they have seems to be date/time stamps and MySQL format and handling.  This seems to be especially relevant to UK coders where our dates take the format dd-mm-yyyy whereas MySQL stores the date in yyyy-mm-dd HH:mm:ss.

PHP is a wonderful language that makes working with dates etc actually very easy.  To resolve the format of date/times from MySQL to a more UK centric format I created a very simple function

function mysql2uk($date) {
      return date('d-m-Y H:i:s',strtotime($date));
} 
<?php
function mysql2uk($date) {
 return date('d-m-Y H:i:s',strtotime($date));
} 
$sql ="select * from table";
$result = mysql_query($sql) or die();
$values = mysql_fetch_row($result);
echo mysql2uk($values['column']);
?>
And as easy as that your date is now in a UK format.

You can edit the format by changing the ‘d-m-Y H:i:s” part of the function and to save you having to have the function at the top of everyone of your pages you can put it in an include file and just include that file on your site/pages where you want the function available.

Hope this little snippet helps some.

Small Basic Opensource Jobboard

A while ago a friend asked if I could knock together a very basic job board for him just to post 4-5 roles he was currently recruiting for.

Being the good friend I am I agreed and 10 minutes later he had his job board.

It recently came about that others may find such a thing useful as well.  To this end I’m posting the Job Board zip file to this blog. Feel free to download it and use it if you want.  There is no license attached to this so do with it what you will.

Word of warning though to those of you that may be expecting an all singing and dancing system – this isn’t it! this is very basic and very simple and served its purpose.  There is no support with these files.

opensource-basic-jobboard

Good Luck

Dave

EZ-Shop approved on Hotscripts and Scripts dungeon

Our EZ-Shop open source e-commerce platform has been approved on both the hot scripts website and the scripts dungeon website.

http://www.HotScripts.com/?RID=N782379
http://www.scriptdungeon.com/freephpscript/freescripts9142

We think this is a great step in the right direction to help us make more people aware that there are full featured and free alternatives out there to the commercial offerings that are so frequently missold to people.