/    Sign up×
Community /Pin to ProfileBookmark

Status 400 and geocoding

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[/url]

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” . “&amp;key=” . $this->map_key;

while ($geocode_pending) {
$address = $this->address.”, “.$this->city.”, WA”;
$request_url = $base_url . “&amp;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);
}
}
}
?>[/code]

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[/url]

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[/url]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SodbusterNov 16.2008 — Change "&amp;" on the "$base_url =" and "$request_url =" lines to "&". With those changes it works perfectly for me.
×

Success!

Help @bejitto101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...