How to Geocode an Address on a Web Form

This code will allow you to collect and address (street, city, state, zip, country) and extract the latitude and longitude so that you can build a Google map to display the location being collected. so here is the html form...
<form action="process.php" method="post">
        <div>
        <label for="propertyName">Property Name</label>
        <input type="text" size="40" name="name" id="name" class="formField">
        </div>
        <div>
        <label for="streetAddress">Address</label>
        <input type="text" size="60" name="streetAddress" id="streetAddress" class="formField">
        </div>
        <div>
        <label for="city">City</label>
        <input type="text" size="20" name="city" id="city" class="formField">
        </div>
        <div>
        <label for="state">State/Province</label>
        <input type="text" size="25" name="state" id="state" class="formField">
        </div>            
        <div>
        <label for="zip">Zip/Postal Code</label>
        <input type="text" size="12" name="zip" id="zip" class="formField">
        </div>    
        <div>
        <label for="country">Country</label>
        <select name="country" id="country" class="formField">
        <?php show_countries(); ?>        <!-- I have a countries function that i can reuse across a site without clogging up my html -->
        </select>
        </div>
</form>
Here are the contents of "process.php"
        <?php

        $query_string = $_POST['streetAddress'] . "+" . $_POST['city'] . "+" . $_POST['state'] . "+" . $_POST['zip'] . "+" . $_POST['country'];

        $coords = getLatLong($query_string);
        $lat = $coords['Latitude'];
        $lng = $coords['Longitude'];

        //then do your database insert with the latitude and longitude values
        //You can call these values out to a map div on the front end of your site inside of a database loop
        
        ?>              
Now here is the "getLatLong()" function
<?php
function getLatLong($code){
 $mapsApiKey = 'PUT_YOUR_MAPS_API_KEY_HERE';
 $query = "http://maps.google.com/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
 // This takes your full street address to create the lat/long
 $data = file_get_contents($query);
 // if data returned
 if($data){
  // convert into readable format
  $data = json_decode($data);
  $long = $data->Placemark[0]->Point->coordinates[0];
  $lat = $data->Placemark[0]->Point->coordinates[1];
  return array('Latitude'=>$lat,'Longitude'=>$long);
 }else{
  return false;
 }
}
?>
Post a comment
  1. Matthew Price

    Hello, This was written a couple of years ago at this point and it is possible that the google maps api doesn't support this type of request anymore. I can test it out again and let you know. Matt

    • Trevin Boucher

      That would be great, anything to get me started. Thanks so much.

      • Matthew Price

        Hello So Here is some code that works with the google maps v3 api for sure.

        if ( isset( $_POST['submit'] ) ) :
                // clean you post vars here...ie make sure they are what you expect.  i have omitted this for example
                $query_string = $_POST['streetAddress'] . "+" . $_POST['city'] . "+" . $_POST['state'] . "+" . $_POST['zip'];
                $query = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=' . urlencode( $query_string );
                $request = curl_init();
                curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($request, CURLOPT_URL, $query);
                $contents = curl_exec($request);
                curl_close($request);
                $contents = json_decode( $contents );
                $coords = array();
                $coords['lat'] = $contents->results[0]->geometry->location->lat;       
                $coords['lng'] = $contents->results[0]->geometry->location->lng;  
                // do what ever you need to do with this data and your database
                // print_r( $contents );  this will show you all of the other items you have access to.
        
        endif;
        
  2. Trevin Boucher

    Is this still the best way to do this? I have taken over a project where the developer used this exact code. I used to work but now for some reason it is no longer working correctly.






Real Time Web Analytics ^