PHP ~ get Address from google map and get location example

這範例是用PHP從google抓地址~~~~~並把JSON轉成物件~~取值

-----------------------------------------------------------------------------------------
function getAddressFromLatLng($lat, $lng)
{
$key = GMAP_KEY;
$address = urlencode($address);
$url = "http://maps.google.com/maps/geo?ll=".$lat.",".$lng."&output=json&key=".$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


$data = getAddressFromLatLng(25.0482805352365, 121.56269073486328);
$data = json_decode($data);

$qqq = $data->Placemark[0]->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
$yyy = $data->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName;
-----------------------------------------------------------------------------------------

$qqq = 台北市
$yyy = 松山區


reference:
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/services.html

http://php.net/manual/en/function.json-decode.php

沒有留言: