I have a database that stores clients and employees. The front end for that database uses very old software (coded in vb6 from the late 90s so I can't simply geocode the addresses for insertion upon entry, unless i wanna do some fancy sql work). I am building some more robust reporting software for the data. I want to include google map features in the software that shows where clients and employees live. When a roster of current employees or clients is generated, I also want it to check for new additions and then geocode their addresses into a database table so that I can reference them in later reports. I have no problem with recognizing the new entries or geocoding their locations.
The way the php script works is it checks to see if clients have latitude and longitude information populated already, and if not, get the lat/lng, and then insert it into the database. If there are several new clients/employees, i want it to loop through the result set and insert the relative information. The issue is the google api gets the lat/lng via javascript, so I can't pass that information back into the php script to store it into the database, and I don't really want to use javascript to insert it.
So i'm wondering the best way to go about this. I have been researching and testing for a couple hours now. I figured I could probably pass the variables through ajax techniques, but isn't that going to cause an issue when doing it in the middle of a loop?
Here's some sample code, and I apologize for such a drawn out question!
Javascript snippet of geocode:
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var newlat = results[0].geometry.location.lat();
var newlng = results[0].geometry.location.lng();
alert(results[0].geometry.location);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
php snippet:
$address = "";
$num = 0;
//Google geocode limits requests to 10 per second, limit to 5 total requests at a time.
//If more theres more than 10 clients who have yet to be geocoded, running the database insertion code
//---should provide long enough delay to geocode again and not hit the overflow limit
while ($row = mssql_fetch_array($result) and $num < 5) {
if ($row['lat'] == "") {
//Create address string
$address = $row['Address1'] . " " . $row['City'] . ", NY " . $row['Zip'];
$address = str_replace("'", "", $address);
//Test to show addresses without lat/lng were pulled
echo "<script>codeAddress('" . $address . "');</script>" . $address . "<br />";
$num = $num + 1;
}
}
file_get_contents("http://maps.google.com/maps/api/geocode/json?sensor=false&address=$addtress");