2013-04-04 42 views
0

我想通過使用PHP 5請求Web服務 - Google地理定位API。我有使用PHP進行XML解析的經驗,但是這個使用JSON格式來製作請求。我不知道如何提出請求!我認爲接收請求與XML解析類似!請求谷歌地圖地理定位API

https://developers.google.com/maps/documentation/business/geolocation/

+0

你不解析JSON。你'json_decode()'無論你得到什麼,它給你一個原生的PHP數組/對象,然後你從那裏開始。 – 2013-04-04 14:55:06

回答

0

如果你有一個商業帳戶,通過你的鏈接,谷歌供應幫助的建議。如果您沒有商業賬戶,您將需要使用提供xml結果的使用This API。 即

http://maps.googleapis.com/maps/api/geocode/xml?address=anywhere&sensor=true 

sensor=false 
7

試試這個(結果以JSON格式):

public function getGeoPosition($address){ 
    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false" . 
     "&address=" . urlencode($address); 

    $json = file_get_contents($url); 

    $data = json_decode($json, TRUE); 

    if($data['status']=="OK"){ 
     return $data['results']; 
    } 

} 

print_r(getGeoPosition('chicago,il')); 
+0

任何想法,爲什麼這個要求只會在部分時間內工作?它對我來說已經超時了,而且實際上告訴我它已經超時了。 – mcbeav 2014-08-20 01:48:36

+1

您可以嘗試新的網址: https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA 另請參閱此處的使用限制: https://developers.google.com/maps/documentation/geocoding/#Limits – 2014-08-20 16:52:48