A tutorial for Place search using G-map and OS-map APIs

Tutorials for Google APIs

Created on Feb.19, 2020. @author: Kai Shen, [email protected]

What are Google Maps APIs?

Google places API allows developers to access a wealth of information from Google’s database for over 100 million places including location data, contact information, user ratings and reviews and more.

In this guide we will learn how to collect place information from Google Maps services. Compared with OpenStreetMap, one of the advantages of Google Maps service is the detailed query results, such as ratings, number of reviews, price level, etc. Based on this information, you can evaluate the “popularity” and “quality” of each place based on your own definition.

How to apply a Google Maps API key

A tutorial for Place search using G-map and OS-map APIs_第1张图片

P.s.

  1. By creating an account, you will be given a free 300$ credit to be used for these services which will be expired one year later. Regardless of the situation though, you still need to enter your credit card details for the Maps API to work with your API key.

  2. Warning: DO NOT CLICK on Activate when Google asks you to upgrade to unlimited usage or you will lose your free credits;

  3. In step 2, you have to create and name a project for which you want to use the APIs.

You have to initiate the API key authentication before accessing the API services. For more detailed explanation, please refer to: Google PLace API documentations

Once you get the API Key, fill it in the get_key() function in key.py.

Places search functionalities provided by Google

Suppose you have an area (e.g. multiple regions in Kyoto city) on the map described by geographical coordinates (longitude and latitude), you want to pass this pair of coordinates to google API and get places of interest within this area.

Currently in the APIs in Python supported by Google, there is not yet a functionality to pass a special set of coordinates to a “Search box” to initiate automatic search for the whole area. Thus, we currently adopt the Nearby Search for getting places of interest in a specific region.

To get the complete results of an area, we perform a Nearby Search at the same point as the center of the area, and set the search radius to half the diagonal length of the area. Remember that the search circle defined by the search radius should be large enough to cover the entire area.

A tutorial for Place search using G-map and OS-map APIs_第2张图片

Before initiating Nearby Search API

Here is something you need to know. There is a limitation in the number of results returned by each query to Google Map API service:

A Text Search or a Nearby Search request would let you access directly the response parameters you need, but with one major restriction : they will return a maximum of 20 results per query. Each search can return as many as 60 results, split across three pages, which means that you can call a next_page_token data and get a grand total of 60 results, but then you are stuck.

Therefore, in order to parse the complete POI data from a larger area (such as the attraction areas defined in the Kyoto tourism survey data), I recommend first dividing the area into smaller grids and starting Nearby Search. To do so, in the Examples.py we implemented this with a method region_query() which automatically recursively divide into smaller areas to continue searching when the number of returned results reaches the limit.

A tutorial for Place search using G-map and OS-map APIs_第3张图片
Now you can go into the codes. Please refer to the annotations and comments in manuscripts for more information as well.

A preview of the query results.

They are sent back in json format and can be further parsed into key-value pairs, e.g.dict data structure in Python.

Res = 
{'geometry': {'location': {'lat': 34.97277770000001, 'lng': 135.690642},
                    'viewport': {'northeast': {'lat': 34.97420002989273,
                                               'lng': 135.6919653798927},
                                 'southwest': {'lat': 34.97150037010729, 'lng': 135.6892657201072}}},
       'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png',
       'id': '200310bbfaf7006dcdc2144f88902ad925d30aca',
       'name': '広島のお好み焼き工房三好',
       'opening_hours': {'open_now': True},
       'photos': [{'height': 3024,
                   'html_attributions': [
                       'yuri'],
                   'photo_reference': 'CmRaAAAAV3TJ8OJc6lg8VYzYrBjcPi_KoCVFltX5R0-sKODuF4cBupOZG0s_OLTaF3X7onLTNAwVpNY8xG2FPXqeV812U5Xn2DmWJzQ0lXgK9plMgXgs-Vh37NwMsQYBHU6AN1soEhBXS-zdcndtBaOqTuucImCWGhQ1nt8xMsCZZfQH8S8jOIj0dg_ltg',
                   'width': 4032}],
       'place_id': 'ChIJp45CiMMGAWARsT-kSqn5rb4',
       'plus_code': {'compound_code': 'XMFR+47 Kyoto', 'global_code': '8Q6QXMFR+47'},
       'price_level': 2,
       'rating': 4.4,
       'reference': 'ChIJp45CiMMGAWARsT-kSqn5rb4',
       'scope': 'GOOGLE',
       'types': ['restaurant', 'food', 'point_of_interest', 'establishment'],
       'user_ratings_total': 106,
       'vicinity': '8-1 Katagiharanakakaito, Nishikyo Ward, Kyoto'}
       

Tutorials for OpenStreetMap APIs

Why OpenStreetMap?

OSM is a free editable map of the whole world. OpenStreetMap is built by a community of mappers that contribute and maintain data. As such, OpenStreetMap provides the following key features:

  • Local Knowledge
  • Community Driven
  • Open Data

There is no doubt that Google Maps have better coverage, more data with better quality, but you will also be charged quite a lot if you generate tons of inquires. Thus, in case you want to parse places in a large region, e.g. the whole Kyoto city while detailed place information is not of your interest (only want to know the rough number of places), then OSM is a better choice in that it provides comparatively free usage.

The OSM examples code introduces a simple method to get places from the OSM server that communicate via an Overpass API. Before going into the code tutorial, try playing with the Overpass API with an example to get familiar with the Overpass query

In the tutorial the query makes requests for places within the whole Kyoto city and assigns results into each attraction area by comparing the coordinates of each place with the area boundary.

  • The Overpass query allows you to customize the place search.
  • There are more ways for accessing and parsing places from OSM APIs. You can refer to following links or find more yourself.
    • Loading Data from OpenStreetMap with Python and the Overpass API
    • OSMPythonTools

Comments:

  • Can’t find coordinates of the area you want to search places from?

    • Go to OpenStreetMap, Click Export (on the left top), -> manually select a different area, then you see the coordinates of the boundary which can be defined by yourself.
  • Why did I use place search?
    In my case, I used place search to collect POIs in each attraction area.Specifically, in Gourmet dimension: I parsed:
    ○ Number of restaurants, bars and pubs (using OSM API)
    ○ High-end restaurants1 (using Google maps API)
    In Leisure dimension, I parsed
    ○ Shops and museums


  1. Price level ranges from 3 to 4; ↩︎

你可能感兴趣的:(A tutorial for Place search using G-map and OS-map APIs)