4.4 Google Places: Calling the API and mapping the results

Function to request Places from Google

In order to get Places from Google, you need to make a request. Create a function that makes this call. You want to feed this function the following parameters:

  • title (a name you want to give this group of places)
  • latlng (the center point)
  • radius (how far out you want to search from the center point in meters)
  • types (an array of values that tells Google which places you are requesting. Valid types are listed here.)
  • icon (the url to the icon image)

The function to make the request would then look like this:

Note that at this point, the function makes an AJAX service call to Google, and simply traces the results in the console. Also, the placesRequest function is not called anywhere in the code at this time. Let’s add a call to this function, much like you have done for your createMarker function in previous tutorials. Add the following lines (highlighted 12-13) to the end of the initialize function:

This request is asking for the following:

“I want a set of places from Google that match their category for ‘school’… which I will also call ‘Schools’… within 1500 meters (radius) of this location (latlng)… to be displayed with a school icon (images/school.png)…

Running this yields an output of an array of objects in the developer tools (make sure to choose the console tab):

Clicking on the triangle next to each object reveals what is inside:

Now let’s use the jQuery .each function to loop through the results of the array. Add the highlighted lines below:

Inspect the traced objects, and begin to outline the necessary parameters needed to map the places. Google often returns an object called “geometry”. By default, this typically contains the coordinates for a given location, and can be called as a LatLng object as “geometry.location”. Now, you have the necessary information to create markers for each “place” that google has given you. Add the highlighted lines below to the .each loop you just created:

(sample code)

2 thoughts on “4.4 Google Places: Calling the API and mapping the results

  1. Pingback: Week 9: Mapping | Exploring interfaces

  2. Pingback: Mapping | Musings of a 21st Century Renaissance Man

Leave a Reply