1
votes

This is a random address I have found doesn't work and yet the address exists fine.

"Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK"

Can you guys try geocoding that and tell me if it works for you, I have 13 addresses, 12 work, this 1 doesn't?

This is the code I am running to make the geocode request:

class geocoder{
    static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=true&address=";

    static public function getLocation($address){
        $url = self::$url.urlencode($address);

        $resp_json = self::curl_file_get_contents($url);
        $resp = json_decode($resp_json, true);

        if($resp['status']='OK'){
            return $resp['results'][0]['geometry']['location'];
        }else{
            return false;
        }
    }

    static private function curl_file_get_contents($URL){
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
}    


$address = urlencode("Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK");
$loc = geocoder::getLocation($address);

This is proof the address exists: http://maps.google.co.uk/maps?q=%22Team+Valley+Trading+Estate,+Gateshead,+Tyne+and+Wear+NE92+1DG,+UK%22&hl=en&sll=53.103814,-2.278518&sspn=10.722701,33.815918&t=h&hnear=NE92+1DG,+United+Kingdom&z=16

This is proof that the geocoding class should be returning data:

http://maps.google.com/maps/api/geocode/json?sensor=true&address=Team%20Valley%20Trading%20Estate,%20Gateshead,%20Tyne%20and%20Wear%20NE92%201DG,%20UK

1
I was double endcoding the url... ****** bows head in shame. noobs will be noobs. - Jimmyt1988
I just tried it on my website prototype and the address work perfectly, returning coordinates, administrative_area_level_2 (province/county) and postal code correctly, so the problem should be in your script, maybe it's the urlencode what messes it up, I don't know. I can link my site so you can see my code (although it's not OO and more basic/simple/worse than yours imo, but it works xD) if you request it, I don't want to put the url at first cos it would look like spam xD - aleation
when geocoding I always try to get the most vital information to the begining of the string (country, region, street name, street number), if I don't find anything I remove the last word of the string and try again. - Naryl

1 Answers

2
votes

you are urlencod'ing the address twice so change:

$address = urlencode("Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK");

to

$address = "Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK";

as you have urlencod'ed the address in your getLocation function