当我运行这段代码时,我得到了一个空白屏幕,什么都没有显示。为了正确无误,我必须进行哪些更改?我在哪里出错?

     <html>
     <head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script>
   <script>
   $(document).ready(function() {
   $.getJSON("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyC1BIAzM34uk6SLY40s-nmXMivPJDfWgTc",
  function(data, textStatus){
alert(data);
  $.each(data.results,function(i, name) {;
   $("#placenames").append(i+':'+name.vicinity+'<br/>');
 });
});
});
 </script>
 </head>
  <body>
  <div id="placenames"></div>
  </body>
  </html>

最佳答案

您是否尝试过使用Google Maps Javascript API?它为您完成所有JSONP任务。

这是一个包含您坐标的演示:http://jsfiddle.net/ThinkingStiff/CjfcX/

脚本:

var places = new google.maps.places.PlacesService( document.createElement( 'div' ) ),
    searchRequest = {

        name: 'harbour',
        location: new google.maps.LatLng( -33.8670522, 151.1957362 ),
        radius: 500,
        types: ['food']

    };

places.search( searchRequest, function ( results, status ) {

    var html = '';

    for ( var index = 0; index < results.length; index++ ) {

        html +=
              '<li '
            + 'data-location-id="' + results[index].id + '" '
            + 'data-address="' + results[index].vicinity + '" '
            + 'data-latitude="' + results[index].geometry.location.lat() + '" '
            + 'data-longitude="' + results[index].geometry.location.lng() + '" '
            + 'data-name="' + results[index].name + '">'
            + '<div>' + results[index].name + '</div>'
            + '<div>' + results[index].vicinity + '</div>'
            + '</li>';

    };

    document.getElementById( 'results' ).innerHTML = html;

} );


HTML:

<script src="http://maps.googleapis.com/maps/api/js?libraries=places,geometry&sensor=true"></script>
<ul id="results"></ul>


输出:

关于java - 使用Google API从Json获取地名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8558158/

10-10 22:05