SANR1103219
SANR1103219

Reputation: 143

getting place name from Json using google API

When I run this code I get a blank screen and nothing gets displayed.What changes I have to make in order to get this right and where am I going wrong?

     <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>

Upvotes: 1

Views: 459

Answers (3)

ThinkingStiff
ThinkingStiff

Reputation: 65351

Have you tried using Google Maps Javascript API? It does all the JSONP stuff for you.

Here is a demo with your coordinates: http://jsfiddle.net/ThinkingStiff/CjfcX/

Script:

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>

Output:

enter image description here

Upvotes: 2

mplungjan
mplungjan

Reputation: 177950

Google API does not support the callback/JSONP from a jQuery get/getJSON at this time

To load async, you need to do something like this:

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
}

http://code.google.com/apis/maps/documentation/javascript/basics.html#Async

Upvotes: 1

Matt Cashatt
Matt Cashatt

Reputation: 24208

You have to add this querystring so that it is parsed as jsonp:

&callback=?

See this blog post for more information:

http://www.mattcashatt.com/post/index/Obtaining-and-Parsing-Open-Social-Graph-Data-with-JSONP-and-jQuery

Upvotes: 0

Related Questions