0

Address query

https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurant+in+kolkata&key=API_KEY

RESTAURANT JSON LIST

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.indiacom.com/\"\u003eIndiacom Yellow Pages\u003c/a\u003e"
   ],
   "next_page_token" : "CuQB2wAAALbNaxylA2QNB4pNRsjUxIOYwTSnii1OIwvXqiCPM3736kQZGJ4sEKthpVgvUkskg0ebaLbeE1iNbNKnO7N__X_FpsGprU3o4scQ5aZuUcroSkZVhuWOKvxWHA9IYVXhFmqrVsgG9mjinq-RvANuV6oKzcvnXK09GvdZR0Xp-HINbfoakOVR0TsoOFNCw4UIWrihFSXPJOXeNtTYuImUrPkYKZVt-Y8xMKxr_aOvRR7L0PfcAcXPpSBB2IugIh2K3ESUGMypJD8EuPW1rqqvvYXcMqqHp8iWzq9h1-ytSFl8EhDn2tzr7gU7AkcPORFyLXuiGhSG1bFqUIK2yQb6_fhN-nbym8c17Q",
   "results" : [
      {
         "formatted_address" : "No. 26,Next To Museum,Sudder Street, New Market Area, Jawaharlal Nehru Road, Kolkata, West Bengal 700016, India",
         "geometry" : {
            "location" : {
               "lat" : 22.558687,
               "lng" : 88.350889
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "68f2d647334cdaa6a4063c86347252f5a6ebe4c1",
         "name" : "Zaranj",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "place_id" : "ChIJ24Jtvqd3AjoRFb_Bd8Il8Cw",
         "rating" : 4.2,
         "reference" : "CmRZAAAA9-XMiEF-hYPFZh9kaOBwXQ6R-60XStrKsOEkThALaaeHHmJv6kvEd2JWCh7F9XU9XwyNaNRj6KZQkgGEvhhD8qTIs2fiH-Cng5kmlbIpuQLzQGFgQ8dgnvmL2U1-Uz3REhBb9bJSg-t8S8GDpGUGqlsSGhQnqUVV75IG_AFtVpeV4Xpg2YeTmA",
         "types" : [ "restaurant", "food", "establishment" ]
      },

JS

//Get Restaurant list
function getRestaurants()
{
    var search=$("#search").val();
    var prestring="restaurant+in+";

    var jsonRList="https://maps.googleapis.com/maps/api/place/textsearch/json?query="+prestring+search+"&key=API_KEY";
    alert(jsonRList);//returning the address correctly
    $.getJSON(jsonRList, function (data1) {
        var Rname=data1.results[0].name;
        alert(Rname);//returning nothing

    });
}

Why this javascript is not returning the restaurant name?(testing first one results[0])

What is the wrong in it?

alert(Rname);//returning nothing

1 Answers1

0

I think you can use console.log(), alert() or document.write() as long as the information is deserialized, I am using JSON.parse() for deserializing, eval() is what people used before. Deserializing means to recover the data encoded in json so Javascript can use it as an object. In fact, if you use write.document(someJson) you will see [object object] meaning you have all your data there ready to be used, but you need to JSON.parse() it first. JSON.stringnify() is the opposite of what you want to do, because that function is for serialize the data and then transmit it, then the receptor needs to use JSON.parse() to deserialized it and use it in Javascript. I like to use write.document() in order to see the information, but console log allows you to see what is inside of the object easier. Here, some code example:

// apiData would be the variable with the API information
var externalData = JSON.parse(apiData);

// will output [object object]     
document.write(externalData);

// will output  “     “
document.write('      ');

// will output the latitude
document.write(externalData.results[0].geometry.location.lat);

// will output in the console, you need to type externalData in the console log 
console.log(externalData);

I hope this helps.

Just for clarification I used this answer to respond a similar question in this other forum How to get geocode latitude and longitude from json google map api in variables by javascript?)

Community
  • 1
  • 1
alfmonc
  • 187
  • 5