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;
}
}
?>