Dette burde være tilpasset din DB:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="
http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps</title>
<script src="
http://maps.google.com/maps?file=api&v=2&key=DIN_GOOGLE_NØGLE_HER" type="text/javascript"></script>
</head>
<body onunload="GUnload()">
<button onclick="getCoordinates()">TEST</button>
<pre id="message">## Adresse opdatering:
</pre>
<script type="text/javascript">
function getCoordinates() {
if (GBrowserIsCompatible()) {
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.";
function getUniq(nLen) {
nLen = typeof nLen=="number" ? nLen : 8;
var a = [], c = "9876543210abcdefghijklmnopqrstuvwxy0123456789";
nClen = c.length;
for (var i=0; i<nLen; i++) a[i] = c.charAt( Math.floor(Math.random()*nClen) );
return a.join("");
}
function GeoCoderObject() {
this.id = "GeoObj_" + getUniq() + GeoCoderObject.nGeoCounter++;
this.onRecieve = function(){};
this._oGeoCoder = new GClientGeocoder();
return this;
}
GeoCoderObject.nMaxObjects = 20; // Maximalt antal af samtidige request-objekter
GeoCoderObject.nGeoCounter = 0;
GeoCoderObject.oGeoCoders = {};
var _p = GeoCoderObject.prototype;
_p.getAddress = function(aSearch) {
var xml = "", me = this, search = aSearch[1];
this.nAddressID = aSearch[0];
this._oGeoCoder.getLocations(search, function (result) {
// If that was successful
if (result.Status.code == G_GEO_SUCCESS) {
// Lets assume that the first marker is the one we want
var p = result.Placemark[0].Point.coordinates;
var lat=p[1];
var lng=p[0];
xml = "UPDATE `locations` SET `lat`='" + lat + "', `lng`='" + lng + "' WHERE `id`='" + me.nAddressID + "';\r\n";
}
// ====== Decode the error status ======
else {
var reason="Code "+result.Status.code;
if (reasons[result.Status.code]) {
reason = reasons[result.Status.code]
}
xml = "## Fejl p\u00e5 flg. adresse: '" + search + "' :: [" + reason + "]\r\n";
}
me.onRecieve(xml, me.id);
result = xml = null;
});
}
_p.destroy = function() {
for (var x in this) if (x!="destroy") this[x] = null;
this.destroy = null;
}
var addresses = [
<?
// Ret til korrekte DB-informationer:
$connection = mysql_connect("SERVER", "USER_NAME", "PASS_WORD");
mysql_select_db("DATA_BASE_NAVN", $connection);
$sql = "SELECT `id`, `adresse`, `postnummer`, `by` FROM `folkebiblioteker` WHERE `lat`<1";
$res = mysql_query( $sql ) or die ( mysql_error() );
$a = array();
while ($row=mysql_fetch_assoc($res)) {
$a[] = "[".$row["id"].", \"".$row["adresse"].", ".$row["postnummer"]." ".$row["by"]."\"]";
}
print implode(",\r\n\t", $a);
?>
];
function writeAddress(sXml, sId) {
document.getElementById("message").firstChild.nodeValue += sXml;
GeoCoderObject.oGeoCoders[sId].destroy();
GeoCoderObject.oGeoCoders[sId] = null;
delete GeoCoderObject.oGeoCoders[sId];
GeoCoderObject.nGeoCounter--;
if (addresses.length==0 && GeoCoderObject.nGeoCounter==0) {
document.getElementById("message").firstChild.nodeValue += "\r\n## ================================================================\r\n## Slut p\u00e5 opdatering."
}
else loadGeoCoders();
}
function loadGeoCoders() {
var o = null;
while (addresses.length>0 && GeoCoderObject.nGeoCounter<=GeoCoderObject.nMaxObjects) {
o = new GeoCoderObject();
o.onRecieve = writeAddress;
GeoCoderObject.oGeoCoders[o.id] = o;
o.getAddress(addresses.shift());
}
}
loadGeoCoders();
} else { // display a warning if the browser was not compatible
alert("Sorry, the Google Maps API is not compatible with this browser");
}
}
</script>
</body>
</html>
Læg mærke til, du selv skal sætte din Google-key ind i JavaScript-tagget.
Udfyld også de rigtige DB-informationer ved MySQL-kaldet. Så burde dokumentet fungere =)
Når koordinaterne er hentet, kører du dem som sql-kommando mod databasen i din phpMyAdmin eller, hvad du nu bruger til DB-vedligeholdelse.
Når du har gjort det, reloader du dokumentet i browseren, hvorved evt. adresser, der ikke kunne findes koordinater til ved første forsøg, skrives ind i JS-array'et - og du kan så prøve at hente dem endnu engang. Somme tider lykkes det ikke Google ved første forsøg, men somregel ved andet - og du kan jo altid prøve en tredie.
Er der efter tre forsøg stadig adresser, du ikke kan hente koordinater til, bør du først tjekke stavning - og derefter evt. hente dem manuelt.
Fidusen er, at du kun henter de adresser i databasen, som har et 'lat' felt, hvis værdi er under 1. Det betyder, at du anden og tredie gang ikke henter de adresser, du allerede har fundet koordinater til:
SELECT `id`, `adresse`, `postnummer`, `by` FROM `folkebiblioteker` WHERE `lat`<1