Friday, November 6, 2009

Reverse Geocode

var reasons=[];
reasons[G_GEO_SUCCESS] = "Success";
reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address: No corresponding geographic location could be found for the specified address.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address: The geocode for the given address cannot be returned due to legal or contractual reasons.";
reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";
reasons[403] = "Error 403: Probably an incorrect error caused by a bug in the handling of invalid JSON.";


var map;
var geocoder;
var wholeList;
var delay =100;
var nextAddress = 0;
var tmpArr = [];
var globalFlag =0;

if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();


//for(var index = 9000; index < 9100; index++){ //pausecomp(100); //readFile(index); //} function getAddress(city, next){ geocoder.getLatLng(city, function(point){ geocoder.getLocations(point, function(response){ if (response.Status.code == G_GEO_SUCCESS) { var state,locality; if(response.Placemark != undefined){ place = response.Placemark[0]; var country = place.AddressDetails.Country.CountryNameCode; state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName; if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea == undefined){ locality = city; } else { locality = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; } var xml = '  <marker city="' + locality + '" state="' +state+ '" country="' +country+ '">
';
document.getElementById("message").innerHTML += xml;

//writeFile(locality,state,country);
}
}else{
if (response.Status.code == G_GEO_TOO_MANY_QUERIES) {
nextAddress--;
delay++;
// var xml = '  <marker address="' + city + '" error="' +reason+ '">
';
// document.getElementById("message").innerHTML += xml;
} else {
var reason="Code "+result.Status.code;
if (reasons[result.Status.code]) {
reason = reasons[result.Status.code]
}
var xml = '  <marker address="' + city + '" error="' +reason+ '">
';
document.getElementById("message").innerHTML += xml;
}
}
})
})
next();
}

function theNext() {
if(globalFlag == 0){
readFile();
globalFlag++;
}
//alert(tmpArr.length);
if (nextAddress < tmpArr.length+1) {
setTimeout('getAddress("'+tmpArr[nextAddress]+'",theNext)', delay);
nextAddress++;
} else {
document.getElementById("message").innerHTML += "</markers>";
}
}


function readFile(){
var modurl = 'ReadFile';

var xmlhttp = getXMLHTTPRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && (!xmlhttp.status || xmlhttp.status == 200)) {
tmpArr = xmlhttp.responseText.split('\n');
var len = tmpArr.length;
//getAddress(tmpArr[index]);
}
}
xmlhttp.open("POST", modurl, true);
xmlhttp.send(null);
}
function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}
/*
function writeFile(locality,state,country){
//alert(locality+state+country);
var modurl = 'WriteFile' + "?locality="+locality+"&state="+state+"&country="+country;
var xmlhttp = getXMLHTTPRequest();
xmlhttp.onreadystatechange = function() {
//if (xmlhttp.readyState == 4 && (!xmlhttp.status || xmlhttp.status == 200)) {
//}
}
xmlhttp.open("POST", modurl, true);
xmlhttp.send(null);
}
*/
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
}
// readFile();
theNext();