2014-03-30 68 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 –