取得json時,我們透過json_decode來解譯
同學專案內地址要為中文,我們這次不能跟『Google Geocoding API 透過地址查詢經緯度』文章一樣取得到的地址為英文
所以我們將『Google Geocoding API 透過地址查詢經緯度』的url改成如下方的url
http://maps.googleapis.com/maps/api/geocode/json?address=地址&language=zh-TW
如果想要翻譯成其他國家的語言可以參考『Supported Languages Spreadsheet』
透過var_dump來觀察json內容
程式碼:
<?php
//Unicode
header("Content-Type:text/html; charset=utf-8");
//Get json
$file = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=高雄孔廟&language=zh-TW');
//decode
$m_json = json_decode($file, true);
var_dump($file);
echo '<br/><br/><br/>';
echo $m_json['results'][1]['formatted_address'] . '<br/>';
echo $m_json['results'][0]['geometry']['location']['lat'] .
', ' . $m_json['results'][0]['geometry']['location']['lng'];
?>
執行成果:
參考資料:
http://tw2.php.net/file_get_contents
https://developers.google.com/maps/faq#languagesupport
http://www.php.net/manual/en/function.var-dump.php

