你得到的JSON是使用AJAX檢索的方式。這可以使用hand coded JavaScript完成,但使用jQuery要容易得多。這裏有一個簡單的例子:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({ url: "http://maps.google.com/maps/api/geocode/json",
type: "GET",
dataType: "json",
data: { address: "1600+Amphitheatre+Parkway,+Mountain+View,+CA", sensor: "false" },
success: function(data, textStatus, XMLHttpRequest) {
console.log(textStatus);
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus);
}});
});
</script>
但卻但這將不工作,因爲same origin policy的。解決方法是使用您選擇的服務器端語言(Perl,PHP,ColdFusion,ASP)充當代理。這樣的url值將是「yourproxy.php」,「yourproxy.cfm」,「yourproxy.asp」或其他。該腳本將簡單地接收它收到的請求,充當用戶代理向Google發送請求並檢索響應(也就是上面代碼中的url值),然後將結果發送到您的腳本。
jQuery庫爲您處理JSON處理,或者您可以使用Bob提供的信息以及上面列出的手動滾動AJAX。請注意,手卷AJAX需要相同的代理解決方案才能從Google獲取信息。
另請注意,您鏈接的地理編碼API並不適用於大量的動態查詢。他們會將您引導至API V2 Client Geocoder,API V3 Client Geocoder和Maps API for Flash Client Geocoder。
我使用網絡服務的原因是google.com的apis在中國加載的速度非常緩慢。我可以用google.cn加載地圖api。但出於某種原因,動態加載的geocoder apis始終使用google.com – hao 2010-03-12 01:07:20