<?php
function place2location($place) {
$p = urlencode($place);
$url = "http://maps.google.com/maps/api/geocode/json?address=$p&sensor=false";
$request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$request->setHeader("user-agent", "Uhehehe! (PHP 5.2.6)");
$response = $request->send();
if ($response->getStatus() == 200) {
$body = $response->getBody();
$obj = json_decode($body);
$location = $obj->results[0]->geometry->location;
var_dump($location);
echo "location is OK<br>";
return $location;
}
}
function location2address($location) {
$lat = $location->lat;
$lng = $location->lng;
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false&language=ja";
$request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$request->setHeader("user-agent", "Uhehehe! (PHP 5.2.6)");
$response = $request->send();
if ($response->getStatus() == 200) {
$body = $response->getBody();
$obj = json_decode($body);
echo "<h1>address</h1>";
$address = $obj->results[0]->address_components;
var_dump($address);
return $address;
}
}
function place2address($place) {
echo "point1<br>";
$location = $this->place2location($place);
echo "point2<br>";
echo "point3<br>";
$address = $this->location2address($location);
var_dump($address);
echo "point4<br>";
return $address;
}
?>