I made a geocoding class that would take an address and convert it to lat and lng. I based it off the code I found here: [url]http://code.google.com/support/bin/answer.py?answer=80200&topic=11364
It’s my first attempt to make a class so obviously it has to fail spectacularly (Epicly you might say).
Here’s the class code:
[code=php]<?php
class Geocoder{
public $address, $city, $state, $zip, $lat, $lng, $error;
private $map_key, $map_host;
public function __construct($address, $city, $state, $zip, $key, $map_host=”maps.google.com”){
$this->address = $address;
$this->city = $city;
$this->state = $state;
$this->zip = $zip;
$this->map_key = $key;
$this->map_host = $map_host;
}
public function locate(){
$geocode_pending = true;
$base_url = “http://” . $this->map_host . “/maps/geo?output=xml” . “&key=” . $this->map_key;
while ($geocode_pending) {
$address = $this->address.”, “.$this->city.”, WA”;
$request_url = $base_url . “&q=” . urlencode($address);
$xml = simplexml_load_file($request_url) or die(“url not loading”);
$status = $xml->Response->Status->code;
if (strcmp($status, “200”) == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(“,”, $coordinates);
// Format: Longitude, Latitude, Altitude
$this->lat = $coordinatesSplit[1];
$this->lng = $coordinatesSplit[0];
return true;
} else if (strcmp($status, “620”) == 0) {
// sent geocodes too fast
$delay += 100000;
} else {
// failure to geocode
$geocode_pending = false;
$this->error = “Address ” . $address . ” failed to geocoded. “;
$this->error .= “Received status ” . $status . “n”;
return false;
}
usleep($delay);
}
}
}
?>
The issue with the code is, the status turns out to be 400. So it keels over. Here’s the error I get: “Error: Address 17190 Clear Creek Rd, Poulsbo, WA failed to geocoded. Received status 400 ” You can check it out here: [url]http://bellevue.com/business_entry.php
Any ideas?
Edit: I don’t know if this is related, but I was having an issue with the web server. I was getting “Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration…” so I looked it up and figured it was allow_url_fopen not being on. But since my client uses a 1and1 server, I couldn’t access the php.ini file. So I simply created my own php.ini flie and put it in the base directory of my site. I think, that’s what I’m supposed to do. [url]http://faq.1and1.com/scripting_languages_supported/php/14.html