-2

I'm creating a website that uses Google Geocoding for site registration. At the moment the code I'm using is server side but I would ideally like to have this as a client side script. How do I convert this:

$id= mysqli_real_escape_string($con, $_REQUEST["id"]);
$postcode= mysqli_real_escape_string($con, $_REQUEST["postcode"]);

$mapping = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . $postcode . "&sensor=false" );
$geocodes = json_decode($mapping);

if($geocodes->status == 'OK'){
     $town = $geocodes->results[0]->address_components[1]->long_name;
     $city = $geocodes->results[0]->address_components[4]->long_name;
     $county = $geocodes->results[0]->address_components[2]->long_name;
     $lat = $geocodes->results[0]->geometry->location->lat;
     $lon = $geocodes->results[0]->geometry->location->lng;
}
else{...

Also, I'd like to be able to filter out the Country so does anyone know what number represents this?

user2737457
  • 293
  • 1
  • 5
  • 19

1 Answers1

0

Below is an example javascript (client) side code of using the google geo API to get coordinate/location information on a postal code. I hardcoded a postal-code in the code for demonstration. The result from google is displayed in the DisplayAddress() routine.

<html>
<head>
</head>
<script>

    // Show a locator on the map
    //
    function ShowLocation( postal )
    {
        // Get the geographic location
        var url = "http://maps.google.com/maps/api/geocode/json?address=" + postal + "&sensor=false";
        sendRequest( url, DisplayAddress  );
    }

    function DisplayAddress( url, response )
    {
        alert( response );
    }

    // Make a REST based request
    function sendRequest( url, callback ) 
    {
        var req = createXMLHTTPObject();
        if (!req) 
            return;
        req.open( "GET", url, true);
        req.setRequestHeader('User-Agent','XMLHTTP/1.0');

        req.onreadystatechange = function () 
        {
            if (req.readyState != 4) return;

            if (req.status != 200 && req.status != 206 && req.status != 304) 
            {
                alert( req.status + " " + url );
                return;
            }   
            callback( url, req.responseText );
        }

        if (req.readyState == 4) 
            return;
        req.send( null );
    }

    // Create object for making REST based request
    function createXMLHTTPObject() 
    {
        if (navigator.appName == "Microsoft Internet Explorer") 
        {
                http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
                http = new XMLHttpRequest();
        }
        return http;
    }
</script>
<body onload="ShowLocation( '98607' )">
</body>
</html>

Andrew, Co-Founder of OpenGeoCode.Org

Andrew - OpenGeoCode
  • 2,299
  • 18
  • 15
  • Thanks Andrew but to be honest I don't want the information for a map but rather to store the Town, City, etc, etc into a members database. What I'm doing is using a textbox that the user enters their full UK postcode into, then the code is sent to a seperate PHP script (Shown above) which extracts the information based on the postcode and then enters it into the database. How can I use the Javascript above to do that client side rather than server side? – user2737457 Nov 06 '13 at 18:42
  • Hate to tell you, but google prohibits using the geocoding API w/o displaying it on a map! You can replace the google service with a free service from geonames.org (http://www.geonames.org/export/web-services.html). You can get your results in JSON or XML. Just parse the results to extract the town, city, etc and then pass them to a database query. – Andrew - OpenGeoCode Nov 06 '13 at 19:17
  • I overlooked. The inserting into the db will have to happen on the server side. You can create a PHP web page for the db insertion with a REST interface and then pass the town,city, etc data to the PHP script via the REST interface. You will also want to think on security that someone else doesn't directly call the PHP script. – Andrew - OpenGeoCode Nov 06 '13 at 19:19
  • Thanks for all the information Andrew, I actually just saw something about the Google TOS for Maps, which is quite annoying because this method works so well for my site. What exactly does it mean to show a map? The postcode input box is on my registration page so there is obviously no map shown there. The main purpose of using this was for LON & LAT Coordinates. Is there another way that I can do this using Google which will get around Google's terms? – user2737457 Nov 06 '13 at 19:28
  • Google restricts the use of their geocoding service solely for the purpose of using the lat/lng coordinates to display something on a google map. – Andrew - OpenGeoCode Nov 19 '13 at 19:55