2014-03-30 69 views
0

我是新來的兩個JSON &谷歌地圖。jQuery getJSON()不能與谷歌地圖API

$(function() { 
    $("#search").click(function() { 
     $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=awan%20town&sensor=false", function (result) { 
      $.each(result, function (i, field) { 
       $.("#map-canvas").append(field + " "); 
      }); 
     }); 
    }); 
}); 

:所以,請告訴我,我很想念

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.15&sensor=false"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 

<script> 

    $function({ 
    $("#search").click(function(){ 
     $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=awan%20town&sensor=false",function(result){ 
      $.each(result,function(i,field){ 
       $("#map-canvas").append(field + " "); 
      }); 
     }); 
    }); 
}); 



</script> 
</head> 
<body> 

<button id="search">Search</button> 
<div id="map-canvas" style="width:100%;height:100%;"></div> 
</body> 
</html> 

回答

1

嘗試包DOM準備處理$(function() {...});裏面你的代碼,以確保所有的DOM元素都被正確地執行你的jQuery代碼之前加載你還需要改變:

$.("#search") 
$.("#map-canvas") 

到:

$("#search") 
$("#map-canvas") 

在選擇此處之前,您不需要.

+0

這裏還沒有工作 –

+0

你想從返回的JSON中獲得什麼字段? – Felix

+0

我想我寫的代碼會從這個URL返回所有的,我真的想得到任何特定的領域如何做到這一點 http://maps.googleapis.com/maps/api/geocode/json?address=awan%20town&sensor = false –