<!--
This is an example of geocoding BULK addresses and saving them to database.

use AJaz function
<script language="javascript">
function ajaxFunction(n){
var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}


//REcursivley Added Addresses
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById('mydiv').innerHTML = ajaxRequest.responseText;
document.getElementById('div2').innerHTML = 'Complete: <b>' + (start/row*100).toFixed(2) + "%</b>";
if(start!=row){
start = start +1;
ajaxFunction(start, row); /Recursive Call
}
}
}
ajaxRequest.open("GET", "geocoder.php?add="+n, true);
ajaxRequest.send(null);
}

</script>

 

 

PHP Is below
-->
<?php

if (!isset($_SESSION)) {
session_start();
}
require_once('database.indo')
$key = "your google key"


sleep((rand(8, 10))/10); //Sleeps from 0.8 - 1 second. does so cause google can't take too many request at once.

$arr = $_SESSION[listA];
$username = $_SESSION[username];
//Three parts to the querystring: q is address, output is the format, key is the GAPI key

$i = $_GET['add']; //array key of current address
$add = $arr[$i. "Address"] . ", " . $arr[$i. "City"] . ", " . $arr[$i. "Province"] . ", " . $arr[$i. "Postal"];

 

$address = urlencode($add);



//send Google a URL Request
$url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;

$ch = curl_init(); //Break up URL to extract info

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
curl_close($ch);




//Check if we were successful
if (substr($data,0,3) == "200")
{
$data = explode(",",$data);

$precision = $data[1];
$latitude = $data[2];
$longitude = $data[3];


//add to database
mysql_select_db($database_local, $local);

$query_Results = sprintf("INSERT into ".$_SESSION['username']."_stores (Name, Address, City, Postal, Province, Additional, latitude, longitude, Date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($arr[$i ."Name"], "text"),
GetSQLValueString($arr[$i ."Address"], "text"),
GetSQLValueString($arr[$i ."City"], "text"),
GetSQLValueString($arr[$i ."Postal"], "text"),
GetSQLValueString($arr[$i ."Province"], "text"),
GetSQLValueString($arr[$i ."Additional"], "text"),
$latitude,
$longitude,
GetSQLValueString(date("r"), "text"));
$Results = mysql_query($query_Results) or die(mysql_error());

echo $add;

} else {
echo "Error in geocoding! Http error ".substr($data,0,3);
}