Reverse Geocode from Full Address

<form action="getLatLng.php" method="post">
<label for="propertyAddress">Address</label>
<input type="text" size="60" name="propertyAddress" id="propertyAddress" class="formField">

<label for="propertyCity">City</label>
<input type="text" size="20" name="propertyCity" id="propertyCity" class="formField">

<label for="propertyState">State/Province</label>
<input type="text" size="25" name="propertyState" id="propertyState" class="formField">

<label for="propertyZip">Zip/Postal Code</label>
<input type="text" size="12" name="propertyZip" id="propertyZip" class="formField">

<input type="submit" class="button" name="createProperty" id="createProperty" value="Create Your Property">


the processor looks like this...


/* Google Maps API. You need to get your own API Key */

function getLatLng($myAddress) {
$mapsApiKey = 'YOUR_API_KEY_HERE';
$query = "".urlencode($myAddress)."&output=json&key=".$mapsApiKey;
$data = file_get_contents($query);
// if data returned
// 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);
return false;

$myAddress = $_POST['propertyAddress'] . "+" . $_POST['propertyCity'] . "+" . $_POST['propertyState'] . "+" . $_POST['propertyZip'] . "+" . $_POST['propertyCountry'];

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

$insert = mysql_query( " INSERT INTO locations SET lat = '$lat', lng = '$lng' ");

// redirect to thank you/results page here



You must be logged in to post a comment.

  1. Thanks Danielle. Assuming that Matt, the author got a notification about this too. I actually work with him and will let him know.

    Google maps API is probably part of the issue. This is a pretty old snippet.

  2. Your code don't work. [anyways note the error getLatLng written with two different ways].
    I have the same task for my website but there isn't way to embed geocoding address and mysql_query together. It seems that mysql_query runs faster than geocode and no data is stored in db.

    My actual solution is an external script that works AFTER storing data. I think it's the only way.