Scrape data from google maps using swift and google maps places API

Akshay Devkate
2 min readDec 26, 2020

--

The “Places Search” in “Google Places API” enables you to retrieve list of places along with summery information about each place the out put can be either retrieved in JSON or XML format.

Follow the steps to scrape data

Step 1. Building URL with required parameters.

The required parameters are

API key - Key is required to make a request and validate the transaction. Find more information on how to get a key in the below link.

Input Type - Such as search term , latitude and longitude that identifies the search target and search criteria. And radius which defines how much area you want to scrape data in and around the defined latitude and longitude. The google places API limits the distance of 5000 meters to each API request made.

Please look at the following code to know how to construct a URL for making a request.

Building URL for making request.

Step 2. Make a HTTP request to get the data.

In Swift we use class URLSession to provide and API for downloading data and uploading data to endpoints indicated by URL.

We are using google maps url endpoints. Using the function getData(from url: String ) we pass the url into the function

URLSession.shared.dataTask with completion handlers

Shared class is used with URLSession to provide a shared singleton session object that gives you a reasonable default behavior for creating tasks.

dataTask(with: ,completionHandler: )

Creates a task that retrieves the contents of a URL based on the specified URL request object, and calls a handler upon completion.

JSONDecoder

An object that decodes instances of a data type from JSON objects.

Step 3. Parse json files with decodable protocols and structure

The completion handler contains data response and error. This data is needed to be parsed into json format. You can choose between the data to be parsed from the server. The above code performs parsing of data from the server.

Find full code in repository in the below link

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response