Blog

  • Today, I’ll be talking on how to go about integrating the google places API into a grails application. It is extremely simple and with a couple of steps you can use google places API in to your grails application. 

    First of all What is Google Places API ?

    The Google Places API is a service that returns information about Places — defined within this API as establishments, geographic locations, or prominent points of interest — using HTTP requests. Place requests specify locations as latitude/longitude coordinates. You can search for nearby places with this API & also see the details of those places as well.

    It is the process of 2 simple steps

    1. Generating API Key

    2. Using API key into grails-app.


    Steps to Generate API Key: 

    The Google Places API uses an API key to identify your application. To activate the Places API and create your key:

    1. Visit the APIs console at https://code.google.com/apis/console and log in with your Google Account.

    2. A default project called API Project is created for you when you first log in to the console. 

    3. Click the Services link from the left-hand menu.

    4. Click the Status switch next to the Places API entry. The switch slides to On.

    5. Click API access from the left navigation. Your key is listed in the Simple API Access section.


    Note: The Google Places API has the following query limits:

    1) Users with an API key are allowed 1 000 requests per 24 hour period.

    2) Users who have also verified their identity through the APIs console are allowed 100 000 requests per 24 hour period. A credit card   is required for verification, by enabling billing in the console. Your card will not be charged for use of the Places API.


    The Places API provides two types of searches:

    • Place Searches, which return a list of Places that are near a user's provided location; and
    • Place Details, which returns more detailed information about a specific location.

    A Place Search request is an HTTP URL of the following form: 

    https://maps.googleapis.com/maps/api/place/search/output?parameters

    A Place Details request is an HTTP URL of the following form: 

    https://maps.googleapis.com/maps/api/place/details/output?parameters

    where output may be either of the following values:

    1. json (recommended) indicates output in JavaScript Object Notation (JSON)

    2. xml indicates output as XML


    Add API Key & URL's in Config.groovy

    abc.apiKey='AddYourOwnKeyHere' 
    abc.placeSearchRequest="https://maps.googleapis.com/maps/api/place/search/output?"
    abc.placeDetailsRequest="https://maps.googleapis.com/maps/api/place/details/output?"
    

    Be specific with your output as json or xml   

    • for json use /search/json?
    • for xml use /search/xml?

    Required parameters: 

    1. key — Your application's API key. 

    2. location — The latitude/longitude around which to retrieve Place information. This must be specified as latitude,longitude.

    3. radius — Defines the distance (in meters) within which to return Place results. The maximum allowed radius is 50 000 meters.

    4. sensor — Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. This value must be either true or false.

    Optional parameters:

    1. keyword — A term to be matched against all content that Google has indexed for this Place, including but not limited to name, type, and address, as well as customer reviews and other third-party content.

    2. name — A term to be matched against the names of Places. Results will be restricted to those containing the passed name value.

    3. types — Restricts the results to Places matching at least one of the specified types. Types should be separated with a pipe symbol (type1|type2|etc). See the list of supported types.

    Now you are all set to start using Google Places API. 


    How to use Google Places API in Grails application:

    Inside your grails service use the code below.

    def apiUrl = grailsApplication.config.abc.placeSearchRequest+"location="
    +"-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&"+ 
    "key="+grailsApplication.config.abc.apiKey
    
    /** JSONObject collects all objects inside the URL*/
    JSONObject jsonObject = new JSONObject(IOUtil.urlToString(new URL(apiUrl)))
    
    /** JSONArray collects array of JSON objects with name results*/
    JSONArray urlJsonArray = jsonObject.getJSONArray("results")
    
    for (int i = 0; i <urlJsonArray.length(); i++) {
    JSONObject venuedataJsonObject = new JSONObject()
    JSONObject resultsJsonObject = urlJsonArray.getJSONObject(i)
       log.debug "name"+resultsJsonObject.name
       log.debug "location"+resultsJsonObject.geometry.location
       log.debug "address"+resultsJsonObject.vicinity
       }
     

    Hope it helps !

    Varun Sharma
    varun.sharma@oodlestechnologies.com

    http://oodlestechnologies.com/

Tags: grails , google places

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com