Search code examples
ibm-mobilefirstgoogle-places-apiworklight-adapters

IBM Worklight - Adapter procedure response returns "zero results" from Google Places


I am getting status "ZERO RESULTS" when I invoke HTTP Adapter using Indian latitude and longitude to search for any service like food, malls etc.

Here is my adapter implementation:

 function getGooglePlaces(location,name) {

    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'maps/api/place/search/json',
        headers: {
            Host: 'maps.googleapis.com'
        },
        parameters : {
            'key'       :   My Key,
            'location'  :  location,
            'radius'    :   '10000',
            'sensor'    :   'false',
            'name'      :  name 
        }
    };

    var response = WL.Server.invokeHttp(input); 
    return response;

}

}

The JSON response for indian coordinates.
results is the array of places searched

{
 "html_attributions": [
  ],
 "isSuccessful": true,
 "responseHeaders": {
  "Alternate-Protocol": "443:quic",
  "Cache-Control": "public, max-age=300",
  "Content-Type": "application\/json; charset=UTF-8",
  "Date": "Wed, 12 Feb 2014 15:06:33 GMT",
  "Expires": "Wed, 12 Feb 2014 15:11:33 GMT",
  "Server": "mafe",
  "Transfer-Encoding": "chunked",
  "Vary": "Accept-Language",
  "X-Frame-Options": "SAMEORIGIN",
  "X-XSS-Protection": "1; mode=block"
   },
  "responseTime": 609,
  "results": [
   ],
   "status": "ZERO_RESULTS",
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 609
   }

Solution

  • I have used the following and got a valid response, using a Worklight adapter.

    • Note that I passed name and location as part of the parameters object and not like you did (just for the sake of simplification)
    • I added a type parameter for "restaurant"
    • I increased the radius

    With the above, I got results.
    Looks to me like this is a matter of tweaking the request parameters. Nothing related to Worklight adapters here, as it works.

    GooglePlaces-impl.js

    function getPlaces() {
        var input = {
                method : 'get',
                returnedContentType : 'json',
                path : 'maps/api/place/search/json',
                headers: {
                    Host: 'maps.googleapis.com'
                },
                parameters : {
                    'key'       : 'make-sure-to-place-here-your-SERVER-KEY-generated-by-Google-GCM-console',
                    'location'  : '27.173033, 78.042133',
                    'radius'    : '30000',
                    'sensor'    : 'false',
                    'name'      : 'Taj Mahal',
                    'type'      : 'restaurant',
                }
            };
    
            var response = WL.Server.invokeHttp(input);
            return response;
    }
    

    Procedure response

    {
       "html_attributions": [
       ],
       "isSuccessful": true,
       "responseHeaders": {
          "Alternate-Protocol": "443:quic",
          "Cache-Control": "public, max-age=300",
          "Content-Type": "application\/json; charset=UTF-8",
          "Date": "Fri, 14 Mar 2014 05:45:41 GMT",
          "Expires": "Fri, 14 Mar 2014 05:50:41 GMT",
          "Server": "mafe",
          "Transfer-Encoding": "chunked",
          "Vary": "Accept-Language",
          "X-Frame-Options": "SAMEORIGIN",
          "X-XSS-Protection": "1; mode=block"
       },
       "responseTime": 662,
       "results": [
          {
             "geometry": {
                "location": {
                   "lat": 27.175015,
                   "lng": 78.042155
                }
             },
             "icon": "http:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/generic_business-71.png",
             "id": "dcc9586b99ab0f0471fccecbe2dbb40fdc1fc2b5",
             "name": "Taj Mahal",
             "photos": [
                {
                   "height": 853,
                   "html_attributions": [
                   ],
                   "photo_reference": "CnRnAAAAojrNNKpH2yqjmiph_kNBxiT5DaK_g3N05YtE_mPP5tBrYD8XjyAAz_xEvWAJymfGOeeOTlVgzFUxUeOMQspvoPGogDQyWomDeZsNP7XEW3JsmzmYakDk_vyJBwajiamhluypx6rCDqDCnBWb6JnCLBIQRuBibftSN9xruu2eeZEm5xoUksG5kyFbtZULWpZceNvNhyl72tQ",
                   "width": 1280
                }
             ],
             "rating": 4.6,
             "reference": "CnRrAAAA8n6I_Dnlm9UzwTiaTntjcxR-FysL5Ya26Fdcsdb48XOIxiJDGdd3AiK6iUUti41d1BQ1XnBfZoVMKWZ5QOyVZAW8QyH-xqSY8eaQXuxH0atjzXtuaplht-ww76JtbxQLkJ4SUtFrmrs7ZjmZn-_RhBIQmYTB0_yGd_4hm2bHoIKt5xoULaBq-FsZo51jFdxLV377nHM0cCI",
             "types": [
                "establishment"
             ],
             "vicinity": "Agra"
          },
         ...
         ...
         "status": "OK",
           "statusCode": 200,
           "statusReason": "OK",
           "totalTime": 1088
        }