Finding your Google Places_ID

Posted by in Code Samples, Web maps on .

In my last post, “Google Map API Gets Personal“, I spoke about Google’s new “Signed In Map” feature which lets users login to Google Maps to save places, even when the map is embedded in someone else’s website. But what if the place that you want to highlight doesn’t show up with the option to “Save to Google Maps”?


Google-Map-API-Gets-Personal1In my previous example I saved York University, a prominent landmark in Toronto, from my map, which appeared on the map by default. If you want to highlight a location using a custom pop-up you can also do so. You will see an example in the footer of this page. Google has documentation about how to do most of this in their Sydney example of a “Custom Save Widget” in their “Signed In” map, but it is missing one vital piece of information. Where do you find the required “places_id”?

I created a tool that allows you to search for places and view the associated place_id.

DOMIC Place_id finder

If you want a better idea of how this works, this post show you how you can use Google’s Places API to look-up the Places ID you need in order to make the above example transferable.

This save feature only works on locations that have been verified by Google. I will take you through verifying if your ‘place’ is in Google’s index, how to set up the Google Places API key, and briefly describe how the Google Places Rest API works. The result will be a way to find the places_id required to duplicate the Sydney example for a different location.

Using the Places API

As stated in Google’s documentation, “The Google Places API allows you to query for place information on a variety of categories, such as: establishments, prominent points of interest, geographic locations, and more. You can search for places either by proximity or a text string. A Place Search returns a list of places along with summary information about each place.”

Step 1: Are we there yet? Verifying if your ‘place’ is in Google’s index .

First you will want to verify that the place you are looking for appears in Google’s database.

Go to Google Maps, zoom in on the area near your ‘place’ and, in the search field enter “*”. This will do a generic search that plots all places in Google’s database on the map.

Search for "*" in Google Maps

Search for “*” in Google Maps

If you are there, great. We are on the right track. If it does not, you may want to visit Create and verify a local business on Google – Google Help. The following instructions (and the Sydney example) will only work for places in Google’s database.

Step 2: The Key to Place Searches! How to set up the Google Places API key.

Before you get started with Google’s Places API you will need an API key. Go to the Google APIs Console ( code.google.com/apis/console )

If you are like me, you are already using Google Maps JavaScript API v3 . In order to find a Places ID you will also need to enable the Places API. Click on “APIs & auth” in the left menu, and search for the “Places API”, and make sure it is enabled.

The Places API key will allow you to search for and get information about locations in Google’s places database, and is a required parameter when performing a Places API search.

Now click “Credentials” to see your API key (listed under “Public API access” > “Key for browser applications”). Use this as your key parameter as described below.

Step 3: Finding your place in all this – How the Google Places Rest API works

It is worthwhile taking a look at Google API Documentation. I only touch on it here, but hopefully provide enough info to find the missing “place_id”.

Google’s Places API has 3 types of searches: Nearby Search Requests, Text Search Requests, and Radar Search Requests. The number of results and the amount of detail each of these returns varies. I was only looking for one id, so the nearby search suited me well. If you need a lot of results returned you may need to use the Radar search, however note that it does not return some valuable information such as “name”.

The searches require similar parameters in the following format:

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

where ‘output’ is either json or xml (I used json) and ‘parameters’ include key, location, radius, and a number of other optional parameters. I used keyword, but name would have worked well too.

You can see an example of the syntax by looking at the URL when clicking on the ‘Nearby Search’ or ‘Radar Search’ links below. Remember to use your own API key, as described above.

Compare the results below of a ‘Nearby Search’ vs a ‘Radar Search’.

Results from ‘Nearby Search‘ in JSON format:

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 43.708816,
               "lng" : -79.27600099999999
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "07c1c20661b71deae2e9a4784b3b9cf42571dd7b",
         "name" : "DOMIC Cartographic Solutions",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "place_id" : "ChIJ8SAzp2PO1IkRjuLr8ItkL4s",
         "reference" : "CoQBfwAAAAhlilEYPEJkvw7zLQKSAbI7oWlQleoEwEXOXRK5phYuEvihmEuVaid0aPldirx-t5fbdAMr-l-19cJr2x9z51NL9woPp8y1n0Zkg5ReChOwhUux9wB479aGzrmubXufdQt4KCC6zQ5bXajW6D2OL2Z3IFPmPFdvdc-Lkpw4NgvlEhBmdKuEqWLqWCgZvm97XknuGhTRqOJuFm4J3XDFO_VFIJYo-4TkAQ",
         "scope" : "GOOGLE",
         "types" : [ "establishment" ],
         "vicinity" : "Scarborough"
      }
   ],
   "status" : "OK"
}

Response from Radar Search

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 43.708816,
               "lng" : -79.27600099999999
            }
         },
         "id" : "07c1c20661b71deae2e9a4784b3b9cf42571dd7b",
         "place_id" : "ChIJ8SAzp2PO1IkRjuLr8ItkL4s",
         "reference" : "CoQBfgAAAC_McKLFSaOhWwOXNDuIHonUKpykCjOlALbgqd2bQzrofxJ-dBBsRaiIIiPxIlEtkSmDDxMbjXo4_w46kV_zYSGf2OLvdmCMD-5wb2GDIOPqIq-NBWBYpSG7KUcXNBMbDWmtmL3OYWjEajTRJBqc59GDeYk-KxVmbKgUYm8_zT-ZEhAruyz7O4vX5rcrongOW4qzGhQDzVVhotBglBdKNKdIljLVGujnCg"
      }
   ],
   "status" : "OK"
}

Note the “place_id” value appears in each of the responses, but otherwise the Nearby Search provides a lot more detail, and the Radar Search would potentially return more results.

You now have all the information you need to build a “Signed In Map” like in Google’s Sydney example.  Try swapping in the location and keyword in the above examples to find your business.

This entry was posted in Code Samples, Web maps and tagged , , on by .

About David McCarthy

David is a web developer, and a map designer. He has been working in the web development and cartographic fields in a variety of positions, including design, development and management. He has excellent interpersonal and team leadership skills proven through systems analysis, project management and customer relations. David focuses on the front-end aspects of web development. Responsive design and mapping applications are at the top of the list. He enjoys exploring new technologies, and building upon his mapping, web design and programming skills.

Leave a Reply

Your email address will not be published. Required fields are marked *